-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreverse-linked-list.py
50 lines (39 loc) · 1.05 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
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseList(self, head):
if head is None:
return None
head, last = self.reverseListRecursive(head, head)
return last
def reverseListRecursive(self, head, last):
if head.next is not None:
right, last = self.reverseListRecursive(head.next, head.next)
right.next = head
head.next = None
return head, last
def reverseListIteratively(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
node = head
prev = None
while node is not None:
next_node = node.next
node.next = prev
prev = node
node = next_node
return prev
x = ListNode(1)
x.next = ListNode(2)
x.next.next = ListNode(3)
x.next.next.next = ListNode(4)
sol = Solution()
y = sol.reverseList(x)
print(y.val)
print(y.next.val)
print(y.next.next.val)
print(y.next.next.next.val)