Skip to content

Commit b3bab72

Browse files
Add 'Reverse Linked List'
1 parent 6b2b478 commit b3bab72

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ A collection of LeetCode solutions
3232

3333
[Product of Array Except Self](./src/product_of_array_except_self.py)
3434

35+
[Reverse Linked List](./src/reverse_linked_list.py)
36+
3537
[Same Tree](./src/same_tree.py)
3638

3739
[String Compression](./src/string_compression.py)

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- [ ] Add alternative solution for 'Longest Increasing Subsequence'
99
- [ ] Add binary search solution for 'Longest Increasing Subsequence'
1010
- [ ] Ensure all lines are < 80 characters.
11+
- [ ] Add recursive solution to 'Reverse Linked List'

src/reverse_linked_list.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
206. Reverse Linked List
3+
4+
https://leetcode.com/problems/reverse-linked-list
5+
6+
NOTES
7+
* Reversing a linked list is similar to merging two sorted linked lists, in
8+
that a pointer ('prev') keeps a reference to the previous node. However, we
9+
also need an additional pointer ('next') to keep a reference to the next
10+
node in the sequence.
11+
12+
Example:
13+
14+
1 2 3 4 5
15+
● → ● → ● → ● → ● → ○
16+
17+
18+
Starting with head (1), we store its reference to next (2):
19+
20+
next = curr.next
21+
22+
Next, we update its reference to next to be the previous node (null):
23+
24+
curr.next = prev
25+
26+
1 2 3 4 5
27+
○ ← ● ● → ● → ● → ● → ○
28+
29+
Then, we update the previous node (null) to be the current node (1):
30+
31+
prev = curr
32+
33+
Finally, we update the current node to be the next node (2):
34+
35+
1 2 3 4 5
36+
○ ← ● ● → ● → ● → ● → ○
37+
38+
39+
This process is repeated until the current node is null:
40+
41+
1 2 3 4 5
42+
○ ← ● ← ● ← ● ← ● ← ● ○
43+
44+
"""
45+
46+
from src.classes import ListNode
47+
48+
49+
class Solution:
50+
"""
51+
The time complexity for this solution is O(n) and the space complexity is
52+
O(1).
53+
"""
54+
55+
def reverseList(self, head: ListNode | None) -> ListNode | None:
56+
prev = None
57+
curr = head
58+
# 1. Store the current node's reference to next.
59+
# 2. Update the current node's next to be the previous node.
60+
# 3. Update the previous node to be the current node.
61+
# 4. Update the current node to be the next node (stored in 1).
62+
while curr:
63+
_next = curr.next
64+
curr.next = prev
65+
prev = curr
66+
curr = _next
67+
return prev

tests/test_reverse_linked_list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
206. Reverse Linked List
3+
4+
https://leetcode.com/problems/reverse-linked-list
5+
"""
6+
7+
from unittest import TestCase
8+
9+
from src.reverse_linked_list import Solution
10+
11+
from .utils import create_linked_list_from_list, create_list_from_linked_list
12+
13+
14+
class TestSolution(TestCase):
15+
def test_1(self):
16+
exp = [5, 4, 3, 2, 1]
17+
l = create_linked_list_from_list([1, 2, 3, 4, 5])
18+
ll = Solution().reverseList(l)
19+
assert create_list_from_linked_list(ll) == exp
20+
21+
def test_2(self):
22+
exp = [2, 1]
23+
l = create_linked_list_from_list([1, 2])
24+
ll = Solution().reverseList(l)
25+
assert create_list_from_linked_list(ll) == exp
26+
27+
def test_3(self):
28+
exp = []
29+
l = create_linked_list_from_list([])
30+
ll = Solution().reverseList(l)
31+
assert create_list_from_linked_list(ll) == exp

0 commit comments

Comments
 (0)