Skip to main content

83. Remove Duplicates from Sorted List

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

Python

class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head

pre = head
cur = head.next

while cur:
if cur.val == pre.val:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return head

Go

func deleteDuplicates(head *ListNode) *ListNode {
if head == nil {
return head
}

pre := head
cur := head.Next
for cur != nil {
if pre.Val == cur.Val {

pre.Next = cur.Next
} else {
pre = cur
}
cur = cur.Next
}
return head
}