54. Remove Duplicates from Sorted List
Problem Statement
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.
Example 1:
Input: head = [1,1,2] Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3] Output: [1,2,3]
Solution
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
current = head
while current and current.next:
if current.val == current.next.val:
# Skip the duplicate node
current.next = current.next.next
else:
# Move to the next node if no duplicate is found
current = current.next
return head
Explanation
This problem is straightforward because the linked list is already sorted. This means any duplicate values will be adjacent to each other.
Algorithm:
-
Initialize
current
pointer: Start acurrent
pointer at thehead
of the linked list. -
Iterate and Compare: Iterate through the list as long as
current
andcurrent.next
are notNone
.- If
current.val
is equal tocurrent.next.val
: This indicates a duplicate. To remove the duplicate, we simply bypasscurrent.next
by settingcurrent.next = current.next.next
. We do not advancecurrent
in this case, because the newcurrent.next
might also be a duplicate ofcurrent.val
. - If
current.val
is not equal tocurrent.next.val
: No duplicate is found, so we advancecurrent
tocurrent.next
to check the next pair.
- If
-
Return Head: After the loop finishes, the list will have all duplicates removed, and we return the original
head
(which might have changed if the first few elements were duplicates).
Time and Space Complexity:
- Time Complexity: O(n), where n is the number of nodes in the linked list, as we iterate through the list once.
- Space Complexity: O(1), as we only use a few extra pointers.