-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0083-remove-duplicates-from-sorted-list.py
More file actions
65 lines (48 loc) · 1.3 KB
/
0083-remove-duplicates-from-sorted-list.py
File metadata and controls
65 lines (48 loc) · 1.3 KB
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
61
62
63
64
65
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
tail = False
pointer1 = head
pointer2 = head.next
while not tail:
if pointer1.val == pointer2.val:
if pointer2.next:
pointer2 = pointer2.next
else:
pointer1.next = None
tail = True
else:
pointer1.next = pointer2
if pointer2.next:
pointer1 = pointer1.next
pointer2 = pointer2.next
else:
tail = True
return head
def printLinkedList(node):
print(node.val)
if node.next:
printLinkedList(node.next)
def main():
sol = Solution()
# Example 2
head = ListNode(1)
n1 = ListNode(1)
head.next = n1
n2 = ListNode(2)
n1.next = n2
n3 = ListNode(3)
n2.next = n3
n4 = ListNode(3)
n3.next = n4
# Test cases
head2 = ListNode(1)
printLinkedList(sol.deleteDuplicates(head2))
if __name__ == "__main__":
main()