-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesfromSortedListII.py
More file actions
56 lines (44 loc) · 995 Bytes
/
RemoveDuplicatesfromSortedListII.py
File metadata and controls
56 lines (44 loc) · 995 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
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
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
number=[-3,-1,0,3]
dumyroot = ListNode(0)
head = dumyroot
for num in number:
head.next=ListNode(num)
head=head.next
head = dumyroot.next
print (head)
heaD=False
if head:
if head.next and head.val==head.next.val:
heaD=True
while head.next and head.val==head.next.val:
head=head.next
ptrf=head.next
else:
print (head)
val=None
ptrb=head
while ptrf :
if ptrf.next and ptrf.val==ptrf.next.val:
val=ptrf.val
ptrf=ptrf.next.next
ptrb.next=ptrf
if head.val==val:
head=ptrb
elif ptrf and ptrf.val==val:
ptrf=ptrf.next
ptrb.next=ptrf
if head.val==val:
head=ptrb
else:
ptrf=ptrf.next
ptrb=ptrb.next
# if head.next and head.val==head.next.val:
# ptrb=head
# ptrf=head.next
if heaD:
print (head.next)
print (head)