-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_0082.py
60 lines (46 loc) · 1.49 KB
/
leetcode_0082.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from typing import Optional
# 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: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
# Set to store values of nodes that are duplicates
blacklist = set()
# Dummy node to handle edge cases such as head being a duplicate
dummy = ListNode(0)
dummy.next = head
prev = dummy
current = head
# First pass: Identify all duplicate values
while current and current.next:
if current.val == current.next.val:
blacklist.add(current.val)
current = current.next
# Second pass: Rebuild the list, skipping blacklisted values
current = head
prev = dummy
while current:
if current.val in blacklist:
# Skip over nodes with duplicate values
prev.next = current.next
else:
# Link nodes without duplicate values
prev = current
current = current.next
return dummy.next
# region linked list
head = ListNode(1)
curr = head
for n in [1,2, 2, 3, 4, 5, 5]:
curr.next = ListNode(n)
curr = curr.next
# endregion
s = Solution()
result = s.deleteDuplicates(head)
while result:
print(result.val, end=", ")
result = result.next