0
0
Data Structures Theoryknowledge~6 mins

Common linked list patterns (runner technique) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Working with linked lists can be tricky because you only move forward one step at a time. The runner technique helps solve problems where you need to compare or find elements at different speeds or positions in the list.
Explanation
Basic idea of the runner technique
The runner technique uses two pointers moving through the list at different speeds or starting points. One pointer moves faster or ahead, while the other moves slower or behind. This helps find relationships between nodes without extra memory.
Two pointers moving at different speeds or positions help solve linked list problems efficiently.
Finding the middle of a linked list
One pointer moves one step at a time (slow), and the other moves two steps at a time (fast). When the fast pointer reaches the end, the slow pointer is at the middle. This works because the fast pointer covers twice the distance.
The slow pointer ends at the middle when the fast pointer reaches the list end.
Detecting a cycle in a linked list
Using the same slow and fast pointers, if the list has a cycle, the fast pointer will eventually catch up to the slow pointer inside the loop. If the fast pointer reaches the end, there is no cycle.
Pointers meeting inside the list means a cycle exists.
Finding the start of a cycle
After detecting a cycle, reset one pointer to the list start. Move both pointers one step at a time. Where they meet again is the start of the cycle. This works because the distance from the start to the cycle equals the distance from the meeting point to the cycle start.
Moving pointers at the same speed after detection finds the cycle start.
Finding the nth node from the end
Move one pointer n steps ahead, then move both pointers one step at a time. When the first pointer reaches the end, the second pointer is at the nth node from the end. This avoids counting the list length first.
Advancing one pointer ahead by n steps helps find the nth node from the end.
Real World Analogy

Imagine two runners on a track. One runs faster and the other slower. By watching where they meet or how far apart they are, you can learn about the track's length or if it loops back on itself.

Basic idea of the runner technique → Two runners running at different speeds on the same track
Finding the middle of a linked list → The slower runner reaching halfway when the faster runner finishes the race
Detecting a cycle in a linked list → The faster runner catching up to the slower runner if the track loops
Finding the start of a cycle → Both runners starting together again to find the loop's start point
Finding the nth node from the end → One runner starting ahead by n steps, then both running together to find a position
Diagram
Diagram
┌───────────────┐
│ Linked List    │
│               │
│  o → o → o → o → o → o → o → null
│       ↑       ↑
│      slow    fast
└───────────────┘
Diagram showing two pointers (slow and fast) moving through a linked list at different speeds.
Key Facts
Runner techniqueA method using two pointers moving at different speeds or positions to solve linked list problems.
Slow pointerPointer that moves one step at a time in the linked list.
Fast pointerPointer that moves two steps at a time in the linked list.
Cycle detectionIf fast and slow pointers meet inside the list, a cycle exists.
Middle nodeThe node where the slow pointer stops when the fast pointer reaches the end.
Nth node from endFound by advancing one pointer n steps ahead, then moving both until the first reaches the end.
Code Example
Data Structures Theory
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def find_middle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow.val

# Create linked list: 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

print(find_middle(head))
OutputSuccess
Common Confusions
Believing the slow pointer always points exactly to the middle in even-length lists.
Believing the slow pointer always points exactly to the middle in even-length lists. In even-length lists, the slow pointer stops at the first of the two middle nodes, not exactly the center.
Assuming cycle detection works if pointers never meet immediately.
Assuming cycle detection works if pointers never meet immediately. Pointers may take several steps before meeting inside a cycle; immediate meeting is not required.
Thinking the fast pointer moves twice as fast in terms of time, not steps.
Thinking the fast pointer moves twice as fast in terms of time, not steps. The fast pointer moves two nodes per step, not necessarily twice as fast in real time.
Summary
The runner technique uses two pointers moving at different speeds to solve linked list problems efficiently.
It helps find the middle node, detect cycles, find cycle starts, and locate nodes from the end without extra memory.
Understanding how slow and fast pointers move and meet is key to applying this technique correctly.