0083. Remove Duplicates from Sorted List¶
Problem¶
| Leetcode | 83. Remove Duplicates from Sorted List |
| Difficulty | |
| Tags | Linked List |
Given the
headof a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Examples¶
Constraints¶
0 <= number of nodes <= 300-100 <= Node.val <= 100- The list is guaranteed to be sorted.
Approach¶
Walk the list with a single pointer cur. If cur.next.val == cur.val, skip the next node by cur.next = cur.next.next. Otherwise advance.
Complexity¶
- Time: O(n)
- Space: O(1)
Implementation¶
Go¶
type ListNode struct { Val int; Next *ListNode }
func deleteDuplicates(head *ListNode) *ListNode {
cur := head
for cur != nil && cur.Next != nil {
if cur.Next.Val == cur.Val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return head
}
Java¶
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while (cur != null && cur.next != null) {
if (cur.next.val == cur.val) cur.next = cur.next.next;
else cur = cur.next;
}
return head;
}
}
Python¶
class Solution:
def deleteDuplicates(self, head):
cur = head
while cur and cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
return head
Edge Cases¶
- Empty list / single node: unchanged.
- All duplicates: collapses to one node.
- No duplicates: unchanged.
Related¶
- 82. Remove Duplicates from Sorted List II — delete all nodes that have duplicates.