Linked List Exercise (1) - Middle of the linked list
Entire code
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self, value):
new_node = Node(value)
self.head = new_node
self.tail = new_node
def append(self, value):
new_node = Node(value)
if self.head == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
return True
def find_middle_node(self):
slow = self.head
fast = self.head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
return slow
my_linked_list = LinkedList(1)
my_linked_list.append(2)
my_linked_list.append(3)
my_linked_list.append(4)
my_linked_list.append(5)
print( my_linked_list.find_middle_node().value )
"""
EXPECTED OUTPUT:
----------------
3
"""
Find middle node code
def find_middle_node(self):
slow = self.head
fast = self.head
while fast is not None and fast.next is not None:
slow = slow.next # one pointer move
fast = fast.next.next # two pointer move
return slow
The find_middle_node method is designed to locate and return the middle node in a linked list. This approach uses two pointers, slow and fast, to traverse the list at different speeds, ultimately allowing slow to stop at the middle node when fast reaches the end.
Step-by-Step Explanation:
Initialize Pointers:
slowandfastare both initialized to the head of the linked list (self.head).The
slowpointer will move one node at a time, while thefastpointer will move two nodes at a time.
Loop Condition:
The
whileloop continues as long as bothfastis notNoneandfast.nextis notNone.This condition ensures that
fastcan move two nodes ahead without going out of bounds.
Pointer Movement:
Inside the loop,
slowadvances one node (slow = slow.next), whilefastadvances two nodes (fast = fast.next.next).As a result,
fastreaches the end of the list in half the time it takes forslow, since it moves at double the speed.
Finding the Middle:
When
fastreaches the end (or one node before the end in an even-length list),slowwill be exactly at the middle of the linked list.This is because for every two nodes
fastmoves,slowonly moves one node, effectively dividing the traversal speed by half.
Return the Middle Node:
- Once the loop terminates,
slowpoints to the middle node, which is then returned as the result.
- Once the loop terminates,
Example:
Suppose the linked list has nodes with values 1 -> 2 -> 3 -> 4 -> 5. Here’s how the pointers move:
Initial:
slowat 1,fastat 1.First Loop:
slowmoves to 2,fastmoves to 3.Second Loop:
slowmoves to 3,fastmoves to 5.The loop ends since
fast.nextisNone.The method returns the node where
slowis currently located, which has the value3.
This method works efficiently in O(n) time complexity, where n is the number of nodes, as each pointer only traverses the list once.
Note.
A while loop means that when the condition is True, it repeats again and again. But when it becomes False, the loop stops. In this code, the loop will stop when both fast becomes None and fast.next becomes None.