-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_linked_list.py
70 lines (53 loc) · 1.52 KB
/
reverse_linked_list.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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def linkedListString(head):
current_node = head
linked_list_str = '['
while current_node:
linked_list_str += str(current_node.val)
if current_node.next:
linked_list_str += ', '
current_node = current_node.next
linked_list_str += ']'
return linked_list_str
def reverseList(head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None or head.next is None:
pass
else:
prev = None
curr = head
while curr is not None:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
head = prev
return head
# Some Test cases
if __name__ == "__main__":
node5 = ListNode(5)
node4 = ListNode(4, node5)
node3 = ListNode(3, node4)
node2 = ListNode(2, node3)
head1 = ListNode(1, node2)
node2 = ListNode(2)
head2 = ListNode(1, node2)
head3 = None
# Examples
input = [
(head1, [5,4,3,2,1]),
(head2, [2,1]),
(head3, [])
]
for idx, (head, expected) in enumerate(input):
print("\nExample", idx, ":")
print("Input:", linkedListString(head))
print("Output:", linkedListString(reverseList(head)))
print("Expected:", expected)