""" 0083.1 - Remove Duplicates from Sorted List - Solution 1 - Iterative Pointer Traversal """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""Remove Duplicates from Sorted List Function"""
curr = head
while curr and curr.next:
if curr.val == curr.next.val:
curr.next = curr.next.next
else:
curr = curr.next
return head
#####################################################################################
# Functions
#####################################################################################
def build_list(vals):
"""Helper to build a linked list from a list of values"""
dummy = ListNode(0)
curr = dummy
for v in vals:
curr.next = ListNode(v)
curr = curr.next
return dummy.next
def print_list(head):
"""Helper to print a linked list"""
vals = []
while head:
vals.append(head.val)
head = head.next
print(vals)
def testcase():
"""Test Function"""
print_list(Solution().deleteDuplicates(build_list([1, 1, 2]))) # [1, 2]
print_list(Solution().deleteDuplicates(build_list([1, 1, 2, 3, 3]))) # [1, 2, 3]
print_list(Solution().deleteDuplicates(build_list([]))) # []
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0083.2 - Remove Duplicates from Sorted List - Solution 2 - Recursive Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""Remove Duplicates from Sorted List Function"""
if not head or not head.next:
return head
head.next = self.deleteDuplicates(head.next)
return head.next if head.val == head.next.val else head
#####################################################################################
# Functions
#####################################################################################
def build_list(vals):
"""Helper to build a linked list from a list of values"""
dummy = ListNode(0)
curr = dummy
for v in vals:
curr.next = ListNode(v)
curr = curr.next
return dummy.next
def print_list(head):
"""Helper to print a linked list"""
vals = []
while head:
vals.append(head.val)
head = head.next
print(vals)
def testcase():
"""Test Function"""
print_list(Solution().deleteDuplicates(build_list([1, 1, 2]))) # [1, 2]
print_list(Solution().deleteDuplicates(build_list([1, 1, 2, 3, 3]))) # [1, 2, 3]
print_list(Solution().deleteDuplicates(build_list([]))) # []
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()