-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse Linked List.py
77 lines (55 loc) · 2.62 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
71
72
73
74
75
76
77
# https://leetcode.com/problems/reverse-linked-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def makeNewList(self, head: Optional[ListNode]):
"""
Time Complexity: O(n) where n = length of linked list. I iterate through the linked list once adding all nodes to a list. I go through the list
and add each node's value to a new node.
Space Complexity: O(n) where n = length of new linked list. The new linked list is the original linked list in reverse order.
"""
# Create a list
history = list()
# Iterate linked list
while head != None:
history.append(head)
head = head.next
# Create a new linked list
newLL = ListNode(-1)
newHead = newLL
# Iterate stack
for node in reversed(history):
newLL.next = ListNode(node.val)
newLL = newLL.next
return newHead.next
def swapInPlace(self, head: Optional[ListNode]):
"""
Time Complexity: O(n) where n = length of linked list. The original linked list is traversed through in order to add all nodes to list. After
the nodes are added, the next pointers are reset in the for-loop. In the for-loop each node is linked to the next element inside
the list.
Space Complexity: O(n) where n = length of list. A list is used to temporarily store all nodes until the linked list is reversed
"""
# Create a history
history = list()
# Iterate linked list
while head != None:
history.insert(0, head)
head = head.next
# Iterate list
for idx in range(len(history)):
# If current node isn't at the end of the list, link this node to next node
if idx + 1 < len(history):
history[idx].next = history[idx + 1]
# Set last remaining node's next pointer to none to avoid cycle
else:
history[idx].next = None
# Return head of new linked list
return history[0]
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# If head is empty or by itself, return it
if head is None or head.next is None:
return head
return self.swapInPlace(head)