-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonReverseList.py
47 lines (30 loc) · 1.09 KB
/
AmazonReverseList.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# #baseCondition
# if head is None or head.next is None:
# return head
# else:
# newHead = self.reverseList(head.next)
# tailNode = head.next
# tailNode.next = head
# head.next = None
# return newHead
#baseCondition :
if head is None or head.next is None:
return head
alreadyReversed, curr = head, head.next
alreadyReversed.next = None
while curr is not None :
storeptr = curr.next
curr.next = alreadyReversed
alreadyReversed = curr
curr = storeptr
return alreadyReversed
"""
https://www.youtube.com/watch?v=j6dEFsH_Aqg
"""