forked from minaevd/hackerrank-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_linked_list.py
More file actions
57 lines (42 loc) · 790 Bytes
/
Copy pathreverse_linked_list.py
File metadata and controls
57 lines (42 loc) · 790 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
57
class ListNode:
def __init__(self, v, n):
self.val = v
self.next = n
def reverse(head):
curr = head
prev = None
nxxt = None
while curr != None:
# nxxt = curr.next
# curr.next = prev
# prev = curr
# curr = nxxt
prev = curr
next = curr.next
curr = next
head = prev
def print_list(head):
l = head
while l != None:
print l.val,
l = l.next
print
def main():
q = dict()
for i in [4,3,2,1,0]:
if(i+1 not in q):
q[i+1] = None
q[i] = ListNode(i+1,q[i+1])
print_list(q[0])
reverse(q[0])
print_list(q[4])
main()
"""
1 - 2 - 3 - 4 - 5
1-3-4-5
2-1-3-4-5
2 - 1 - 3 - 4 - 5
3 - 2 - 1 - 4 - 5
...
5 - 4 - 3 - 2 - 1
"""