Skip to content

Commit b75bb86

Browse files
committed
refactor(data-structures, linked list): reorder linked list tests
1 parent 8a19ffb commit b75bb86

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from typing import Optional, List
2-
from ..singly_linked_list.node import SingleNode
1+
from typing import Optional
2+
from datastructures.linked_lists.singly_linked_list.node import SingleNode
33

44

55
def reorder_list(head: Optional[SingleNode]) -> None:
@@ -9,32 +9,54 @@ def reorder_list(head: Optional[SingleNode]) -> None:
99
if not head or not head.next:
1010
return
1111

12-
# first find the middle of the linked list
12+
# First find the middle of the linked list
13+
# Find the middle of the linked list using the two pointer approach, where we move the first
14+
# pointer two nodes forward while moving the slow pointer one node forward. When the fast pointer reaches
15+
# the end of the linked list, the slow pointer will be at the middle node
16+
1317
slow = head
1418
fast = head.next
1519

1620
while fast and fast.next:
1721
slow = slow.next
1822
fast = fast.next.next
1923

20-
# second half of the list
24+
# second half of the linked list will be the next node of the slow pointer
2125
second = slow.next
26+
# cut the connection between the second half and the first half
2227
slow.next = None
28+
# this will keep track of the previous node in the second half of the linked list
2329
previous = None
2430

2531
# reversing the second portion of the list
2632
while second:
33+
# temporarily store the next node
2734
temp = second.next
35+
# set the next pointer to the previous node
2836
second.next = previous
37+
# move the previous node forward
2938
previous = second
39+
# move the second half head forward
3040
second = temp
3141

32-
# merge the 2 halves
42+
# The second half of the linked list has been reversed.
43+
# Now we merge and weave the first half and the second
44+
45+
# first_half_head will point to the head node of the first half
3346
first = head
47+
# Set the second_half_head to previous as previous will now be the head of the second half of the linked list
3448
second = previous
3549

3650
while second:
51+
# save the next pointers of both nodes to not lose track of them
3752
temp1, temp2 = first.next, second.next
53+
54+
# Assign the first half head node's next to the second half head node
55+
# And assign the second half next node to the first half next node
56+
# This is where the merging and weaving happens
57+
3858
first.next = second
3959
second.next = temp1
60+
61+
# Move the pointers forward for further iterations
4062
first, second = temp1, temp2

tests/datastructures/linked_list/test_reorder_list.py renamed to datastructures/linked_lists/reorder_list/test_reorder_list.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55

66
class ReorderListTestCase(unittest.TestCase):
7-
@unittest.skip(
8-
"Skipping due to a missing dunder method on __repr__ & __eq__ on the SingleNode class"
9-
)
107
def test_1_2_3_4_returns_1_4_2_3(self):
118
"""Should return 1 -> 4 -> 2 -> 3 from 1 -> 2 -> 3 -> 4"""
129
head = SingleNode(
1310
1, next_=SingleNode(2, next_=SingleNode(3, next_=SingleNode(4)))
1411
)
15-
expected = SingleNode(
12+
expected_head = SingleNode(
1613
1, next_=SingleNode(4, next_=SingleNode(2, next_=SingleNode(3)))
1714
)
15+
expected = [1, 4, 2, 3]
1816
reorder_list(head)
19-
self.assertEqual(expected, head)
20-
self.assertEqual(expected, head)
2117

22-
@unittest.skip(
23-
"Skipping due to a missing dunder method on __repr__ & __eq__ on the SingleNode class"
24-
)
18+
actual = []
19+
current = head
20+
while current:
21+
actual.append(current.data)
22+
current = current.next
23+
24+
self.assertEqual(expected_head, head)
25+
self.assertEqual(expected, actual)
26+
2527
def test_1_2_3_4_5_returns_1_5_2_4_3(self):
2628
"""Should return 1 -> 5 -> 2 -> 4 -> 3 from 1 -> 2 -> 3 -> 4 -> 5"""
2729
head = SingleNode(
@@ -30,14 +32,24 @@ def test_1_2_3_4_5_returns_1_5_2_4_3(self):
3032
2, next_=SingleNode(3, next_=SingleNode(4, next_=SingleNode(5)))
3133
),
3234
)
33-
expected = SingleNode(
35+
expected_head = SingleNode(
3436
1,
3537
next_=SingleNode(
3638
5, next_=SingleNode(2, next_=SingleNode(4, next_=SingleNode(3)))
3739
),
3840
)
41+
expected = [1, 5, 2, 4, 3]
42+
3943
reorder_list(head)
40-
self.assertEqual(expected, head)
44+
45+
actual = []
46+
current = head
47+
while current:
48+
actual.append(current.data)
49+
current = current.next
50+
51+
self.assertEqual(expected_head, head)
52+
self.assertEqual(expected, actual)
4153

4254

4355
if __name__ == "__main__":

datastructures/linked_lists/singly_linked_list/single_linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ def reorder_list(self) -> Optional[SingleNode]:
10151015
return None
10161016

10171017
# first split the linked list into two halves. To do this without knowing the length of the linked list beforehand
1018-
# we must first find the middle node. This uses the slow & fast pointer approach
1018+
# we must first find the middle node. This uses the slow and fast pointer approach
10191019
middle_node = self.middle_node()
10201020

10211021
# Store the second half head node

datastructures/linked_lists/singly_linked_list/single_linked_list_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def merge_and_weave(
66
first_half_head: SingleNode, second_half_head: SingleNode
77
) -> Optional[SingleNode]:
88
"""
9-
Merges and weaves the first half and the reversed second half of the linked list in place.
9+
Merges and weaves the first half and the second half of a linked list in place.
1010
Args:
1111
first_half_head: head node of the first half of the linked list
1212
second_half_head: head node of the reversed second half of the linked list

tests/datastructures/linked_list/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)