0
0
DSA Pythonprogramming

Find Middle Element of Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
To find the middle, use two pointers moving at different speeds so when the fast one reaches the end, the slow one is in the middle.
Analogy: Imagine walking on a path with a friend: you walk one step at a time, your friend runs two steps at a time. When your friend finishes the path, you are halfway through.
Head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
Slow ↑
Fast ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> 5 -> null
Goal: Find the middle element of the linked list
Step 1: Initialize slow and fast pointers at head (node 1)
1 -> 2 -> 3 -> 4 -> 5 -> null
Slow ↑1
Fast ↑1
Why: Both pointers start at the beginning to begin traversal
Step 2: Move slow by 1 step to node 2, fast by 2 steps to node 3
1 -> [Slow->2] -> [Fast->3] -> 4 -> 5 -> null
Why: Slow moves one step, fast moves two steps to find middle faster
Step 3: Move slow by 1 step to node 3, fast by 2 steps to node 5
1 -> 2 -> [Slow->3] -> 4 -> [Fast->5] -> null
Why: Continue moving pointers until fast reaches the end
Step 4: Fast cannot move two steps further (end reached), stop
1 -> 2 -> [Slow->3] -> 4 -> 5 -> null
Fast at end
Why: Fast pointer reached the end, slow pointer is at middle
Result:
1 -> 2 -> [3] -> 4 -> 5 -> null
Middle element is 3
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        new_node = Node(val)
        if not self.head:
            self.head = new_node
            return
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = new_node

    def find_middle(self):
        slow = self.head
        fast = self.head
        while fast and fast.next:
            slow = slow.next  # advance slow by one
            fast = fast.next.next  # advance fast by two
        return slow.val if slow else None

# Driver code
ll = LinkedList()
for value in [1, 2, 3, 4, 5]:
    ll.append(value)
middle = ll.find_middle()
print(f"Middle element is {middle}")
slow = self.head fast = self.head
Initialize slow and fast pointers at the start of the list
while fast and fast.next:
Loop until fast pointer reaches the end or one before end
slow = slow.next # advance slow by one
Move slow pointer one step forward
fast = fast.next.next # advance fast by two
Move fast pointer two steps forward
return slow.val if slow else None
Return the value at slow pointer which is the middle
OutputSuccess
Middle element is 3
Complexity Analysis
Time: O(n) because we traverse the list once with the fast pointer moving twice as fast
Space: O(1) because we only use two pointers regardless of list size
vs Alternative: Naive approach counts nodes first then traverses again to middle, costing O(n) + O(n) = O(n), this method finds middle in one pass
Edge Cases
Empty list
Returns None since there is no middle
DSA Python
return slow.val if slow else None
List with one element
Returns the single element as middle
DSA Python
while fast and fast.next:
List with even number of elements
Returns the second middle element (e.g., for 4 elements returns 3rd node)
DSA Python
while fast and fast.next:
When to Use This Pattern
When you need to find the middle of a linked list in one pass, use two pointers moving at different speeds to find it efficiently.
Common Mistakes
Mistake: Moving both pointers by one step each time
Fix: Move fast pointer by two steps and slow pointer by one step to find middle in one pass
Mistake: Not checking if fast or fast.next is None before moving fast
Fix: Add condition 'while fast and fast.next' to avoid errors at list end
Summary
Finds the middle element of a linked list using two pointers moving at different speeds.
Use when you want the middle node without counting total nodes first.
The fast pointer moves twice as fast as the slow pointer, so when fast reaches the end, slow is in the middle.