For this submission, I attempted 83. Remove Duplicates from Sorted List.
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
My initial attempt at this problem only deleted adjacent duplicate nodes; however, to solve this problem correctly, we have to keep deleting nodes until we have no duplicates.
Here is my solution:
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
temp = head
if not head:
return None
while head.next is not None:
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next
return temp
Top comments (0)