@@ -5,14 +5,6 @@ class Node:
55 Attributes:
66 data (int): The data stored in the node.
77 next (Node | None): A reference to the next node in the linked list.
8-
9- >>> head = Node(4)
10- >>> head.next = Node(2)
11- >>> head.next.next = Node(1)
12- >>> head.next.next.next = Node(3)
13- >>> sorted_head = merge_sort_linked_list(head)
14- >>> print_linked_list(sorted_head)
15- 1 2 3 4
168 """
179
1810 def __init__ (self , data : int ) -> None :
@@ -22,39 +14,63 @@ def __init__(self, data: int) -> None:
2214
2315def get_middle (head : Node | None ) -> Node | None :
2416 """
25- Find the middle node of the linked list using the slow and fast pointer technique.
17+ Find the node before the middle of the linked list
18+ using the slow and fast pointer technique.
2619
2720 Parameters:
2821 head: The head node of the linked list.
2922
3023 Returns:
31- The middle node of the linked list, or None if the list is empty.
24+ The node before the middle of the linked list,
25+ or None if the list has fewer than 2 nodes.
3226
3327 Example:
3428 >>> head = Node(1)
3529 >>> head.next = Node(2)
3630 >>> head.next.next = Node(3)
3731 >>> middle = get_middle(head)
38- >>> middle.data if middle else None
32+ >>> middle.data
3933 2
40- >>> get_middle(None) is None
41- True
4234 """
43-
4435 if head is None or head .next is None :
45- return head
36+ return None
37+
38+ slow : Node = head
39+ fast : Node | None = head .next
4640
47- slow : Node | None = head
48- fast : Node | None = head
4941 while fast is not None and fast .next is not None :
50- if slow is None : # This should never happen, but it satisfies the type checker
51- return None
5242 slow = slow .next
5343 fast = fast .next .next
5444
5545 return slow
5646
5747
48+ def print_linked_list (head : Node | None ) -> None :
49+ """
50+ Print the linked list in a single line.
51+
52+ Parameters:
53+ head: The head node of the linked list.
54+
55+ Example:
56+ >>> head = Node(1)
57+ >>> head.next = Node(2)
58+ >>> head.next.next = Node(3)
59+ >>> print_linked_list(head)
60+ 1 2 3
61+ """
62+
63+ current = head
64+ first = True # To avoid printing space before the first element
65+ while current :
66+ if not first :
67+ print (" " , end = "" )
68+ print (current .data , end = "" )
69+ first = False
70+ current = current .next
71+ print ()
72+
73+
5874def merge (left : Node | None , right : Node | None ) -> Node | None :
5975 """
6076 Merge two sorted linked lists into one sorted linked list.
@@ -91,32 +107,6 @@ def merge(left: Node | None, right: Node | None) -> Node | None:
91107 return result
92108
93109
94- def print_linked_list (head : Node | None ) -> None :
95- """
96- Print the linked list in a single line.
97-
98- Parameters:
99- head: The head node of the linked list.
100-
101- Example:
102- >>> head = Node(1)
103- >>> head.next = Node(2)
104- >>> head.next.next = Node(3)
105- >>> print_linked_list(head)
106- 1 2 3
107- """
108-
109- current = head
110- first = True # To avoid printing space before the first element
111- while current :
112- if not first :
113- print (" " , end = "" )
114- print (current .data , end = "" )
115- first = False
116- current = current .next
117- print ()
118-
119-
120110def merge_sort_linked_list (head : Node | None ) -> Node | None :
121111 """
122112 Sort a linked list using the Merge Sort algorithm.
@@ -135,22 +125,6 @@ def merge_sort_linked_list(head: Node | None) -> Node | None:
135125 >>> sorted_head = merge_sort_linked_list(head)
136126 >>> print_linked_list(sorted_head)
137127 1 2 3 4
138-
139- >>> head = Node(1)
140- >>> head.next = Node(2)
141- >>> head.next.next = Node(3)
142- >>> head.next.next.next = Node(4)
143- >>> sorted_head = merge_sort_linked_list(head)
144- >>> print_linked_list(sorted_head)
145- 1 2 3 4
146-
147- >>> head = Node(10)
148- >>> head.next = Node(3)
149- >>> head.next.next = Node(5)
150- >>> head.next.next.next = Node(1)
151- >>> sorted_head = merge_sort_linked_list(head)
152- >>> print_linked_list(sorted_head)
153- 1 3 5 10
154128 """
155129
156130 # Base Case: 0 or 1 node
@@ -159,17 +133,18 @@ def merge_sort_linked_list(head: Node | None) -> Node | None:
159133
160134 # Split the linked list into two halves
161135 middle = get_middle (head )
162- if middle is None :
136+ if middle is None or middle . next is None :
163137 return head
138+
164139 next_to_middle = middle .next
165140 middle .next = None # Split the list into two parts
166141
167- left = merge_sort_linked_list (head ) # Sort left half
168- right = merge_sort_linked_list (next_to_middle ) # Sort right half
142+ # Recursively sort both halves
143+ left = merge_sort_linked_list (head )
144+ right = merge_sort_linked_list (next_to_middle )
169145
170146 # Merge sorted halves
171- sorted_list = merge (left , right )
172- return sorted_list
147+ return merge (left , right )
173148
174149
175150if __name__ == "__main__" :
0 commit comments