-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_likedlist.py
108 lines (84 loc) · 1.97 KB
/
reverse_likedlist.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Node:
def __init__(self, val=0):
self.val=val
self.next=None
#-------------------------
def traverse(head):
current_node=head
result=[]
while current_node:
result.append(current_node.val)
current_node=current_node.next
return result
#----------------------------
def reverse(head):
back=head
new_end=back
cur=back.next
next=cur.next
while back and cur and next:
prev=next
cur.next=back
try:
print('prev', prev.val)
except:
print('prev is none')
print('cur ', cur.val)
print('cur next ', cur.next.val)
back=next.next
prev.next=cur
try:
print('prev.next', prev.next.val)
except:
print('prev next is none')
cur=back.next
next=cur.next
new_head=prev
if back and not cur and not next:
back.next=prev
new_head=back
if back and cur and not next:
cur.next=back
back.next=prev
new_head=cur
print('cur', cur.val)
print('cur.next', cur.next.val)
print('back', back.val)
print('back.next', back.next.val)
new_end.next=None
print(traverse(new_head))
#----------------------------
def reverse_3(head):
previous, current, next = None, head, None
while current is not None:
next = current.next
current.next = previous
previous = current
current = next
print(traverse(previous))
#----------------------------
def reverse_2(head):
back=None
cur=head
while cur is not None:
next_n=cur.next
cur.next=back
back=cur
cur=next_n
print(traverse(back))
#----------------------------
## create a list
#-------------------
a=Node(1)
b=Node(2)
c=Node(3)
d=Node(4)
e=Node(5)
f=Node(6)
a.next=b
b.next=c
c.next=d
d.next=e
e.next=f
print(traverse(a))
reverse_2(a)