Skip to content

Commit 37d41a7

Browse files
committed
Use dataclasses, use iter to remove print function
Refactor Node class to use dataclass for attributes and update iter_linked_list to yield node data. Remove print_linked_list function and update merge function to use direct comparison.
1 parent 9215334 commit 37d41a7

1 file changed

Lines changed: 43 additions & 36 deletions

File tree

‎data_structures/linked_list/merge_sort_linked_list.py‎

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1+
from collections.abc import Iterable
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass(order=True)
16
class Node:
27
"""
38
A class representing a node in a linked list.
49
510
Attributes:
6-
data (int): The data stored in the node.
7-
next (Node | None): A reference to the next node in the linked list.
11+
data: The data stored in the node.
12+
next: A reference to the next node in the linked list.
13+
"""
14+
15+
data: int
16+
next: Node | None = None
17+
18+
19+
def iter_linked_list(head: Node | None) -> Iterable[Node]:
820
"""
21+
Iterate over the nodes of a linked list.
22+
23+
Parameters:
24+
head: The head node of the linked list.
25+
26+
Yields:
27+
Each node in the linked list, one by one.
928
10-
def __init__(self, data: int) -> None:
11-
self.data = data
12-
self.next: Node | None = None
29+
Example:
30+
>>> head = Node(3, Node(1, Node(2)))
31+
>>> head # dataclasses provide a nice .__repr__().
32+
Node(data=3, next=Node(data=1, next=Node(data=2, next=None)))
33+
>>> tuple(iter_linked_list(head))
34+
(3, 1, 2)
35+
"""
36+
current = head
37+
while current:
38+
yield current.data
39+
current = current.next
1340

1441

1542
def get_middle(head: Node | None) -> Node | None:
@@ -47,32 +74,6 @@ def get_middle(head: Node | None) -> Node | None:
4774
return slow
4875

4976

50-
def print_linked_list(head: Node | None) -> None:
51-
"""
52-
Print the linked list in a single line.
53-
54-
Parameters:
55-
head: The head node of the linked list.
56-
57-
Example:
58-
>>> head = Node(1)
59-
>>> head.next = Node(2)
60-
>>> head.next.next = Node(3)
61-
>>> print_linked_list(head)
62-
1 2 3
63-
"""
64-
65-
current = head
66-
first = True # To avoid printing space before the first element
67-
while current:
68-
if not first:
69-
print(" ", end="")
70-
print(current.data, end="")
71-
first = False
72-
current = current.next
73-
print()
74-
75-
7677
def merge(left: Node | None, right: Node | None) -> Node | None:
7778
"""
7879
Merge two sorted linked lists into one sorted linked list.
@@ -87,19 +88,23 @@ def merge(left: Node | None, right: Node | None) -> Node | None:
8788
Example:
8889
>>> left = Node(1)
8990
>>> left.next = Node(3)
91+
>>> tuple(iter_linked_list(left))
92+
(1, 3)
9093
>>> right = Node(2)
9194
>>> right.next = Node(4)
95+
>>> tuple(iter_linked_list(right))
96+
(2, 4)
9297
>>> merged = merge(left, right)
93-
>>> print_linked_list(merged)
94-
1 2 3 4
98+
>>> tuple(iter_linked_list(merged))
99+
(1, 2, 3, 4)
95100
"""
96101

97102
if left is None:
98103
return right
99104
if right is None:
100105
return left
101106

102-
if left.data <= right.data:
107+
if left <= right:
103108
result = left
104109
result.next = merge(left.next, right)
105110
else:
@@ -124,9 +129,11 @@ def merge_sort_linked_list(head: Node | None) -> Node | None:
124129
>>> head.next = Node(2)
125130
>>> head.next.next = Node(1)
126131
>>> head.next.next.next = Node(3)
132+
>>> tuple(iter_linked_list(head))
133+
(4, 2, 1, 3)
127134
>>> sorted_head = merge_sort_linked_list(head)
128-
>>> print_linked_list(sorted_head)
129-
1 2 3 4
135+
>>> tuple(iter_linked_list(sorted_head))
136+
(1, 2, 3, 4)
130137
"""
131138

132139
# Base Case: 0 or 1 node

0 commit comments

Comments
 (0)