-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemove-Duplicates.py
More file actions
26 lines (22 loc) · 859 Bytes
/
Remove-Duplicates.py
File metadata and controls
26 lines (22 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
# sentinel
sentinel = ListNode(0, head)
# predecessor = the last node
# before the sublist of duplicates
pred = sentinel
while head:
# if it's a beginning of duplicates sublist
# skip all duplicates
if head.next and head.val == head.next.val:
# move till the end of duplicates sublist
while head.next and head.val == head.next.val:
head = head.next
# skip all duplicates
pred.next = head.next
# otherwise, move predecessor
else:
pred = pred.next
# move forward
head = head.next
return sentinel.next