-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC - Reverse Linked List.py
68 lines (59 loc) · 1.99 KB
/
LC - 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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
#Iterative Method | Runtime: 40 ms | Memory Usage: 15.4 MB
"""
Reverse Linked List - Iterative - Flow
head = [Null,1,2,3,4,5] 1st 2nd 3rd 4th 5th
temp = current.next (2) (3) (4) (5) (null)
current.next = previous (Null) (1) (2) (3) (4)
previous = current (1) (2) (3) (4) (5)
current = temp (2) (3) (4) (5) (null)
"""
class Solution(object):
def reverseList(self, head):
previous = None
current = head
while current != None:
temp = current.next
current.next = previous
previous = current
current = temp
return previous
#Recursive Method | Runtime: 28 ms | Memory Usage: 18.7 MB
"""
Reverse Linked List - Recursive - Flow
head = [Null,1,2,3]
newH = curr (1)
newH = self._reverse(curr.next) (2)
|___newH = curr(2)
newH = self._reverse(curr.next) (3)
|___newH = curr(3)
curr.next = None
return newH(3)
____|
newH = 3 *FROM RETURN
curr.next.next (3-->None) = curr(2) *SO 3-->2
curr.next(2-->None)
return newH(3-->2)
____|
newH = 3 *FROM RETURN
curr.next.next(2-->None) = curr(1) *SO 2-->1
curr.next(1-->None)
return newH(3-->2-->1-->None/Null)
"""
class Solution(object):
def reverseList(self, head):
if head is None:
return head
return self._reverse(head)
def _reverse(self, curr):
newH = curr
if curr is None: return None
if curr.next:
newH = self._reverse(curr.next)
curr.next.next = curr
curr.next = None
return newH