0
0
Data Structures Theoryknowledge~10 mins

Common linked list patterns (runner technique) in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common linked list patterns (runner technique)
Start: Initialize two pointers
Set slow = head, fast = head
Move fast pointer twice as fast as slow
Check if fast or fast.next is None?
YesStop: End of list reached
No
Move slow by one step, fast by two steps
Repeat until fast reaches end
Use slow pointer position for desired operation
The runner technique uses two pointers moving at different speeds to traverse a linked list, enabling efficient detection of midpoints, cycles, or other patterns.
Execution Sample
Data Structures Theory
slow = head
fast = head
while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
return slow
This code finds the middle node of a linked list by moving 'fast' pointer twice as fast as 'slow'.
Analysis Table
Stepslow Pointerfast PointerCondition fast and fast.nextActionList State (Nodes)
1Node1Node1TrueMove slow to Node2, fast to Node3Node1 -> Node2 -> Node3 -> Node4 -> Node5
2Node2Node3TrueMove slow to Node3, fast to Node5Node1 -> Node2 -> Node3 -> Node4 -> Node5
3Node3Node5FalseStop loopNode1 -> Node2 -> Node3 -> Node4 -> Node5
Exit--fast or fast.next is NoneLoop ends, slow at middle Node3Middle node found at Node3
💡 fast reached the end of the list (Node5), so condition fast and fast.next is False, loop stops.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3 (End)
slowNode1Node2Node3Node3
fastNode1Node3Node5Node5
Key Insights - 3 Insights
Why does the loop stop when fast or fast.next is None?
Because fast moves two steps at a time, if fast or fast.next is None, it means we've reached the end or there is no next node to jump to, so continuing would cause an error. See execution_table row 3.
Why do we move slow by one step but fast by two steps?
Moving fast twice as fast allows slow to reach the middle when fast reaches the end. This difference in speed helps find the midpoint efficiently, as shown in execution_table steps 1 and 2.
What if the list has an even number of nodes? Where does slow end?
In an even-length list, slow will stop at the first of the two middle nodes because the loop condition fails when fast or fast.next is None. This is consistent with the stopping condition in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, where is the slow pointer?
ANode3
BNode2
CNode5
DNode1
💡 Hint
Check the 'slow Pointer' column at Step 2 in the execution_table.
At which step does the loop condition become false?
AStep 3
BStep 2
CStep 1
DExit
💡 Hint
Look at the 'Condition fast and fast.next' column in execution_table where it changes to False.
If fast moved only one step each time instead of two, how would the slow pointer's position change at Step 2?
AIt would be at Node3
BIt would be at Node2
CIt would be at Node4
DIt would be at Node5
💡 Hint
Consider that slow moves one step per iteration and fast moves one step instead of two; slow would advance more slowly relative to fast.
Concept Snapshot
Runner Technique for Linked Lists:
- Use two pointers: slow and fast
- Move slow by 1 step, fast by 2 steps
- Loop until fast or fast.next is None
- Slow ends at middle node (or near middle)
- Useful for finding midpoints, detecting cycles
Full Transcript
The runner technique uses two pointers moving at different speeds through a linked list. Initially, both pointers start at the head. In each loop iteration, the slow pointer moves one node forward, while the fast pointer moves two nodes forward. The loop continues as long as the fast pointer and its next node exist. When the fast pointer reaches the end of the list, the slow pointer will be at the middle node. This method efficiently finds the midpoint of the list without needing to know its length in advance. The execution table shows step-by-step how slow and fast pointers move through the nodes, and the variable tracker records their positions after each step. Key moments clarify why the loop stops and why the pointers move at different speeds. The visual quiz tests understanding of pointer positions and loop conditions. This technique is a common pattern in linked list problems.