Find Middle of Linked List — Concurrent Mutation Bug
In a production playlist shuffle, concurrent mutation caused middle to return last node, skipping first half.
20+ years shipping performance-critical code where algorithms decide the bill. Notes here come from systems that actually shipped.
- ✓Basic programming fundamentals
- ✓A computer with internet access
- ✓Willingness to follow along with examples
- Use two pointers: slow moves 1 step, fast moves 2 steps per iteration
- When fast reaches the end, slow lands on the middle node
- Handles both odd and even length lists without counting nodes
- Time O(n), space O(1) — no extra data structures
- Works on unknown-length lists, making it ideal for streaming data
Before we find the middle of something, we need to be crystal clear on what that something actually is.
An array stores elements in one contiguous block of memory — like numbered seats in a cinema row. A linked list is different. Each element lives in its own separate box called a node, and each node holds two things: the actual data (say, a number or a name), and a pointer — basically an arrow — that points to the next node in the chain.
The last node's arrow points to null, meaning 'the chain ends here'.
Think of it like a treasure hunt. Each clue (node) tells you the answer AND gives you directions to the next clue. You can't skip to clue 7 without following clues 1 through 6 first. That's the key constraint.
In Java, a single node looks like this: a class with an integer value field and a next field that points to another node. The entire list is represented by just one thing — a reference to the very first node, called the head. If you lose the head reference, you lose the whole list.
Imagine two friends start walking along a long road at the same time — one walks normally, and the other walks exactly twice as fast. When the fast walker reaches the end of the road, the slow walker is exactly at the middle. That's it. That's the whole algorithm. A linked list is just a chain of boxes connected by arrows, and we use this same 'two walkers' trick to find the middle box without counting all the boxes first.
Finding the middle of a linked list is one of those problems that looks trivial until you actually sit down to solve it. You can't just do list[length/2] like you would with an array — linked lists don't give you random access. Every node only knows about the next node, so you have to walk the chain step by step. This constraint shows up constantly in real software: playlist management, undo history stacks, browser cache chains, and operating system task queues all use linked structures where random access is expensive or impossible.
The naive solution — count all nodes, then walk to the middle — works, but it requires two full passes over the list. In performance-critical code, two passes when one will do is a red flag. The elegant solution uses two pointers moving at different speeds, finding the middle in a single pass. This 'fast and slow pointer' pattern doesn't just solve this one problem either — it's the foundation for detecting cycles in linked lists, finding the nth node from the end, and several other classic problems you'll see in interviews.
By the end of this article, you'll be able to write a clean, single-pass solution to find the middle of any linked list from memory. You'll understand exactly why it works (not just that it works), spot the two most common mistakes beginners make, and confidently answer interview questions on this topic. Let's build it from the ground up.
Why Finding the Middle of a Linked List Is a Concurrency Trap
Finding the middle node of a singly linked list is a classic two-pointer problem: move a slow pointer one step and a fast pointer two steps per iteration. When the fast pointer reaches the end, the slow pointer lands at the middle. This runs in O(n) time with O(1) extra space — optimal for a single-threaded traversal.
In practice, the algorithm assumes the list structure is immutable during traversal. If another thread concurrently mutates the list — inserting, deleting, or rearranging nodes — the fast pointer can skip over or land on a node that no longer exists, causing a NullPointerException or an infinite loop. The two-pointer technique is not atomic; between the slow and fast moves, the list can change.
Use this algorithm only when you control the list’s lifecycle — e.g., during initialization, in a single-threaded context, or under a read lock. In concurrent systems, prefer a snapshot or a synchronized traversal. Many teams discover this the hard way when a seemingly simple utility crashes under load.
How to Find the Middle of a Linked List — Algorithm
Use the fast/slow pointer (tortoise and hare) technique to find the middle in O(n) time with O(1) space — no need to count length first.
Algorithm: 1. Initialize slow = head, fast = head. 2. While fast is not null and fast.next is not null: a. slow = slow.next. b. fast = fast.next.next. 3. When the loop ends, slow is at the middle.
Worked example — list: 1->2->3->4->5: Step 1: slow=1, fast=1. Step 2: slow=2, fast=3. Step 3: slow=3, fast=5. fast.next = null. Loop ends. Middle = slow = node 3.
For even length (1->2->3->4): Step 1: slow=1, fast=1. Step 2: slow=2, fast=3. fast.next.next = null. Loop ends. Middle = node 2 (first of two centers). To get the second center, change condition to while fast.next is not null.
This technique is used in merge sort on linked lists and palindrome checking on linked lists.
What Is a Linked List — A Quick Ground-Up Recap
Before we find the middle of something, we need to be crystal clear on what that something actually is.
An array stores elements in one contiguous block of memory — like numbered seats in a cinema row. A linked list is different. Each element lives in its own separate box called a node, and each node holds two things: the actual data (say, a number or a name), and a pointer — basically an arrow — that points to the next node in the chain. The last node's arrow points to null, meaning 'the chain ends here'.
Think of it like a treasure hunt. Each clue (node) tells you the answer AND gives you directions to the next clue. You can't skip to clue 7 without following clues 1 through 6 first. That's the key constraint.
In Java, a single node looks like this: a class with an integer value field and a next field that points to another node. The entire list is represented by just one thing — a reference to the very first node, called the head. If you lose the head reference, you lose the whole list.
package io.thecodeforge.linkedlist; // A single node in a linked list. // Each node holds a piece of data AND a reference to the next node. public class LinkedListNode { int data; // The actual value stored in this node LinkedListNode next; // Arrow pointing to the next node (null if this is the last node) // Constructor: creates a new node with the given value. // When first created, next is automatically null — it points nowhere yet. public LinkedListNode(int data) { this.data = data; this.next = null; } // --- Quick demo to visualise a simple linked list --- public static void main(String[] args) { // Build the list: 10 -> 20 -> 30 -> 40 -> 50 -> null LinkedListNode head = new LinkedListNode(10); // head always points to the first node head.next = new LinkedListNode(20); head.next.next = new LinkedListNode(30); head.next.next.next = new LinkedListNode(40); head.next.next.next.next = new LinkedListNode(50); // Walk the list from head to tail and print each value LinkedListNode current = head; // start at the beginning System.out.print("List: "); while (current != null) { // keep going until we fall off the end System.out.print(current.data + " -> "); current = current.next; // move to the next node } System.out.println("null"); } }
head) doesn't hold the whole list — it holds only the address of the first node. Everything else is reachable by following the chain of .next pointers. Lose the head, lose everything.The Naive Approach — Count First, Then Walk (Two Passes)
The first solution most beginners reach for is completely logical: count all the nodes to get the total length, divide by two to get the middle index, then walk to that index. Nothing wrong with that thinking — it works correctly every time.
Here's how it plays out: if the list has 5 nodes, middle index = 5/2 = 2 (zero-based), so the third node is the middle. If the list has 6 nodes, there are technically two middle nodes — convention in most interviews and problems is to return the second middle (index 3), but confirm this with your interviewer.
The downside is that this makes two full passes over the list. For a list with a million nodes, that's two million pointer hops. In a performance-sensitive environment — a real-time system, or a function called thousands of times per second — that cost adds up. More importantly, in an interview, proposing a two-pass solution before a one-pass solution signals that you haven't pushed your thinking far enough.
Still, write the naive solution first. It shows you understand the problem, and it gives you a correctness baseline to test against your optimised version.
package io.thecodeforge.linkedlist; public class FindMiddleNaive { // --- Node definition (self-contained for easy copy-paste) --- static class Node { int data; Node next; Node(int data) { this.data = data; } } // Finds the middle node using two passes. // Pass 1: count how many nodes exist. // Pass 2: walk to the middle index. static Node findMiddleTwoPass(Node head) { if (head == null) { return null; // empty list — nothing to find } // --- Pass 1: Count all nodes --- int totalNodes = 0; Node counter = head; // use a separate pointer so we preserve head while (counter != null) { totalNodes++; // count this node counter = counter.next; // move to the next node } // Calculate the middle index. // For even-length lists (e.g., 6 nodes), 6/2 = 3, which gives the SECOND middle. // Integer division in Java truncates, so 5/2 = 2 (third node, zero-based) — correct. int middleIndex = totalNodes / 2; // --- Pass 2: Walk to the middle index --- Node current = head; for (int step = 0; step < middleIndex; step++) { current = current.next; // take one step forward } return current; // this node is the middle } // --- Helper: build a linked list from an array of values --- static Node buildList(int[] values) { Node dummyHead = new Node(0); // placeholder to simplify building Node tail = dummyHead; for (int value : values) { tail.next = new Node(value); tail = tail.next; } return dummyHead.next; // return actual first node, not the dummy } public static void main(String[] args) { // Test 1: Odd number of nodes (clear single middle) Node oddList = buildList(new int[]{10, 20, 30, 40, 50}); Node middleOdd = findMiddleTwoPass(oddList); System.out.println("Odd list (10->20->30->40->50): Middle = " + middleOdd.data); // Test 2: Even number of nodes (two possible middles — we return the second) Node evenList = buildList(new int[]{10, 20, 30, 40, 50, 60}); Node middleEven = findMiddleTwoPass(evenList); System.out.println("Even list (10->20->30->40->50->60): Middle = " + middleEven.data); // Test 3: Single node — should return that node itself Node singleNode = buildList(new int[]{42}); Node middleSingle = findMiddleTwoPass(singleNode); System.out.println("Single node list (42): Middle = " + middleSingle.data); } }
totalNodes / 2 with integer division handles this automatically. Always clarify with your interviewer which middle they want.The Elegant Solution — Fast and Slow Pointers (One Pass)
Here's where it gets beautiful. Remember the two-walkers analogy? One walker moves one step at a time — that's the slow pointer. The other moves two steps at a time — that's the fast pointer. Both start at the head. You keep moving them until the fast pointer reaches the end of the list. At that exact moment, the slow pointer is sitting right in the middle.
Why does this work mathematically? When fast has travelled N steps, slow has travelled N/2 steps. If fast reaches the end (N = total length), slow is at position N/2 — the middle. It's essentially doing the division for you, in real-time, without ever counting the length.
The one subtle detail is the stopping condition. You stop when fast reaches null (the end) OR when fast.next is null (fast is on the last node). If you only check fast != null, you risk fast jumping to null and then trying to access fast.next on the next iteration — which throws a NullPointerException. This is the most common mistake beginners make with this algorithm, so pay close attention to the stopping condition in the code below.
This pattern — the fast/slow pointer — is one of the most versatile tools in DSA. Once you internalize it here, you'll recognize it in cycle detection, palindrome checking, and reordering problems.
package io.thecodeforge.linkedlist; public class FindMiddleFastSlow { // --- Node definition --- static class Node { int data; Node next; Node(int data) { this.data = data; } } // Finds the middle node in ONE pass using the fast/slow pointer technique. // // VISUAL for list 10->20->30->40->50: // // Start: slow=10, fast=10 // Step 1: slow=20, fast=30 (slow moves +1, fast moves +2) // Step 2: slow=30, fast=50 (slow moves +1, fast moves +2) // fast.next is null, so we STOP. slow=30 is the middle. ✓ // static Node findMiddleFastSlow(Node head) { if (head == null) { return null; // empty list guard } Node slowPointer = head; // moves 1 step at a time Node fastPointer = head; // moves 2 steps at a time // Keep moving until fast pointer reaches the end. // We check BOTH conditions to avoid NullPointerException: // - fastPointer != null -> fast hasn't fallen off the end // - fastPointer.next != null -> fast can safely take TWO steps while (fastPointer != null && fastPointer.next != null) { slowPointer = slowPointer.next; // slow takes 1 step fastPointer = fastPointer.next.next; // fast takes 2 steps } // When the loop ends, slowPointer is exactly at the middle. return slowPointer; } // --- Helper: print list values for easy visualisation --- static void printList(Node head) { Node current = head; while (current != null) { System.out.print(current.data); if (current.next != null) System.out.print(" -> "); current = current.next; } System.out.println(); } // --- Helper: build list from int array --- static Node buildList(int[] values) { Node dummy = new Node(0); Node tail = dummy; for (int val : values) { tail.next = new Node(val); tail = tail.next; } return dummy.next; } public static void main(String[] args) { // Test 1: 5 nodes — middle is the 3rd node (30) Node list1 = buildList(new int[]{10, 20, 30, 40, 50}); System.out.print("List 1: "); printList(list1); System.out.println("Middle: " + findMiddleFastSlow(list1).data); System.out.println(); // Test 2: 6 nodes — two middles, we return the SECOND (40) Node list2 = buildList(new int[]{10, 20, 30, 40, 50, 60}); System.out.print("List 2: "); printList(list2); System.out.println("Middle: " + findMiddleFastSlow(list2).data); System.out.println(); // Test 3: 1 node — the only node is the middle Node list3 = buildList(new int[]{99}); System.out.print("List 3: "); printList(list3); System.out.println("Middle: " + findMiddleFastSlow(list3).data); System.out.println(); // Test 4: 2 nodes — second node is the middle (even list convention) Node list4 = buildList(new int[]{5, 15}); System.out.print("List 4: "); printList(list4); System.out.println("Middle: " + findMiddleFastSlow(list4).data); } }
Stepping Through the Algorithm — Visual Dry Run
Reading code is one thing; watching it execute step by step is how it actually sticks. Let's dry-run the fast/slow pointer algorithm on a 5-node list so there's zero ambiguity about what's happening at each moment.
Our list: 10 → 20 → 30 → 40 → 50 → null
Before the loop starts, both slow and fast are pointing at node 10 (the head). Now the loop checks: fast (10) is not null, and fast.next (20) is not null — condition passes, we enter the loop.
Iteration 1: slow moves to 20, fast jumps to 30. Check: fast (30) is not null, fast.next (40) is not null — condition passes.
Iteration 2: slow moves to 30, fast jumps to 50. Check: fast (50) is not null, but fast.next is null — condition FAILS. Loop exits.
Slow is at 30 — the middle node. Done.
Now let's check the even-length case: 10 → 20 → 30 → 40 → null (4 nodes). Start: slow=10, fast=10. Iter 1: slow=20, fast=30. Iter 2: slow=30, fast=null. fast is null — loop exits. Slow is at 30, which is the second of the two middle nodes (30 and 20). This matches the expected convention perfectly.
package io.thecodeforge.linkedlist; public class DryRunVisualised { static class Node { int data; Node next; Node(int data) { this.data = data; } } // Same algorithm as before, with step-by-step print statements // so you can watch EXACTLY what happens on each iteration. static Node findMiddleWithTrace(Node head) { if (head == null) return null; Node slowPointer = head; Node fastPointer = head; int step = 0; System.out.printf("%-8s %-12s %-12s%n", "Step", "Slow", "Fast"); System.out.printf("%-8s %-12s %-12s%n", "------", "----------", "----------"); System.out.printf("%-8s %-12s %-12s%n", "Start", slowPointer.data, fastPointer.data); while (fastPointer != null && fastPointer.next != null) { slowPointer = slowPointer.next; fastPointer = fastPointer.next.next; step++; // Print current state after each move System.out.printf("%-8s %-12s %-12s%n", "Step " + step, slowPointer.data, (fastPointer != null ? fastPointer.data : "null")); } System.out.println(); return slowPointer; } static Node buildList(int[] values) { Node dummy = new Node(0); Node tail = dummy; for (int val : values) { tail.next = new Node(val); tail = tail.next; } return dummy.next; } public static void main(String[] args) { System.out.println("=== ODD LIST: 10 -> 20 -> 30 -> 40 -> 50 ==="); Node oddList = buildList(new int[]{10, 20, 30, 40, 50}); Node result1 = findMiddleWithTrace(oddList); System.out.println("Middle node value: " + result1.data); System.out.println(); System.out.println("=== EVEN LIST: 10 -> 20 -> 30 -> 40 ==="); Node evenList = buildList(new int[]{10, 20, 30, 40}); Node result2 = findMiddleWithTrace(evenList); System.out.println("Middle node value: " + result2.data); } }
When to Use Fast/Slow Pointer in Production
The fast/slow pointer technique isn't just for coding interviews. It shows up in real systems wherever you need to find a midpoint without knowing the total size in advance.
Playlist shuffle. Streaming audio or video services often use linked lists for queues. When the user hits shuffle, the algorithm needs to split the list at its midpoint to interleave halves. Using two passes would introduce perceptible lag for playlists with thousands of tracks.
Memory defragmentation. Some custom allocators manage free space as a linked list. Finding the middle to balance fragmentation requires a single-pass midpoint search — the fast/slow pointer is a natural fit.
Database query result pagination. When a query returns a cursor-based linked set of rows, the middle page can be found efficiently without counting the total result count (which may be expensive).
The key constraint in all these cases: you don't know the total size beforehand, and you cannot afford two traversals. The fast/slow pointer delivers the middle in a single pass with zero extra memory.
Two Passes? Fine for Homework, Not for Production
The naive approach is exactly what it sounds like: count every node in a first pass, then walk to the halfway point in a second pass. It works. It passes LeetCode. But it's also a clear signal you haven't thought about the problem deeply.
Here's why it stinks: you're touching every node twice. In a production system serving thousands of requests per second, that doubles your cache misses, doubles your pointer chasing, and doubles the time your thread spends fighting for memory bandwidth. When the list lives across scattered heap locations (which it always does in real workloads), the second pass is a full cache reload.
Time complexity is O(n) — same as the fast/slow pointer — but the constant factor is 2x. Space is O(1). That's the only good thing you can say about it. If you're writing a one-off script to inspect a debug payload, fine. If you're shipping this in a latency-sensitive path, your on-call rotation will hate you.
// io.thecodeforge — dsa tutorial public class TwoPassMiddle { static class Node { int data; Node next; Node(int data) { this.data = data; } } public static Node findMiddle(Node head) { if (head == null) return null; int count = 0; Node current = head; while (current != null) { count++; current = current.next; } int mid = count / 2; current = head; for (int i = 0; i < mid; i++) { current = current.next; } return current; } public static void main(String[] args) { Node head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); head.next.next.next.next = new Node(50); System.out.println("Middle: " + findMiddle(head).data); } }
Fast and Slow Pointer: The One-Pass Hack That Actually Works
This is the hare and tortoise algorithm, and it's one of those rare patterns that's both elegant and brutally practical. Two pointers. One moves one step per iteration, the other moves two. When the fast pointer hits the end, the slow pointer is dead center. That's it.
Why does this work? Because the fast pointer covers distance at double the rate. When it's traversed the whole list (length L), the slow pointer has covered L/2 — exactly the middle. For even-length lists, this lands on the second middle node because the fast pointer needs exactly L/2 steps to reach null, and slow moves L/2 times. Simple arithmetic.
The real win: you do it in one pass. No counting, no second traversal, no reloading nodes from cold cache. In production, that translates directly to fewer microseconds per request. When your list is chaining through a high-throughput pipeline, those microseconds compound into saved dollars.
Edge cases? Null head returns null. Single node returns the same node. The algorithm naturally handles both — no special-case checks needed.
// io.thecodeforge — dsa tutorial public class FastSlowMiddle { static class Node { int data; Node next; Node(int data) { this.data = data; } } public static Node findMiddle(Node head) { if (head == null) return null; Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } public static void main(String[] args) { Node head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); head.next.next.next.next = new Node(50); head.next.next.next.next.next = new Node(60); System.out.println("Middle: " + findMiddle(head).data); } }
The Real Middle Problem: Concurrency and Mutability
Here's what no tutorial tells you: finding the middle of a linked list in a single-threaded vacuum is trivial. The moment you put this logic in a production service where other threads are concurrently inserting, deleting, or moving nodes, your "middle" becomes a moving target.
Imagine this: your fast pointer is two steps ahead, but a concurrent thread deletes the node it's about to land on. Now you're either dereferencing a stale pointer (segfault in native code) or reading garbage. Even with garbage collection, you can get corrupted data if the list structure mutates mid-traversal.
We solved this at my last gig by using immutable linked list versions — copy-on-write semantics for the critical path. If you can't afford that, at least guard with a read-write lock. And never, ever assume the list length is stable between two traversals. That naively counted length from pass one? It could be wrong by the start of pass two.
Bottom line: the algorithm is correct in math. In a real system, correctness depends on your memory model and locking strategy. Don't forget that.
// io.thecodeforge — dsa tutorial import java.util.concurrent.locks.ReentrantReadWriteLock; public class ConcurrentMiddle { static class Node { int data; volatile Node next; Node(int data) { this.data = data; } } private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); public Node findMiddleSafe(Node head) { lock.readLock().lock(); try { if (head == null) return null; Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } finally { lock.readLock().unlock(); } } }
Missing Middle in Playlist Shuffle
- Never traverse a mutable linked list without synchronization in a multithreaded context.
- When data structure integrity is critical, consider materializing the list into an array for algorithms that require structural assumptions.
- The fast/slow pointer technique is not thread-safe by itself — protect the list with a lock or use immutable snapshots.
while (fast != null && fast.next != null). The second check is essential to avoid accessing next on null when fast lands on the last node.while (fast.next != null && fast.next.next != null). Clarify requirements with your team.if (head == null) return null;. Then trace the loop with debug output to see where pointers stop.Add `System.out.println("Step: slow=" + slow.data + ", fast=" + fast.data);` inside the while loopCheck the while condition: standard returns second middle, modified returns first10 -> 20 -> 30 -> 40. If slow ends at 30, that's second middle. If at 20, that's first.`if (head == null) return null;` — add this guardFor single node, fast stays at head (not null), fast.next is null — the while condition must allow that casewhile (fast != null && fast.next != null) – this correctly handles 1-node list because fast.next is null, loop skips, returns head| Aspect | Two-Pass (Count + Walk) | One-Pass (Fast/Slow Pointers) |
|---|---|---|
| Number of passes over list | 2 full passes | 1 pass (fast pointer covers ~n/2 extra nodes) |
| Time Complexity | O(n) | O(n) — same class, but ~half the constant |
| Space Complexity | O(1) | O(1) |
| Code complexity | Simple to read, easy to reason about | Slightly trickier stopping condition |
| Works on unknown-length lists? | Yes — counting finds the length | Yes — no length needed at all |
| Interview preference | Acceptable as a starting point | Expected as the optimised answer |
| Risk of NullPointerException | Low — single pointer, simple loop | Medium — must check fast AND fast.next |
| Handles empty list? | Yes, if you guard for null head | Yes, if you guard for null head |
| File | Command / Code | Purpose |
|---|---|---|
| LinkedListNode.java | public class LinkedListNode { | What Is a Linked List |
| FindMiddleNaive.java | public class FindMiddleNaive { | The Naive Approach |
| FindMiddleFastSlow.java | public class FindMiddleFastSlow { | The Elegant Solution |
| DryRunVisualised.java | public class DryRunVisualised { | Stepping Through the Algorithm |
| TwoPassMiddle.java | public class TwoPassMiddle { | Two Passes? Fine for Homework, Not for Production |
| FastSlowMiddle.java | public class FastSlowMiddle { | Fast and Slow Pointer |
| ConcurrentMiddle.java | public class ConcurrentMiddle { | The Real Middle Problem |
Key takeaways
while (fastPointer != null && fastPointer.next != null)Common mistakes to avoid
3 patternsChecking only `fastPointer != null` in the while condition
fastPointer.next.next when fast lands exactly on the last nodewhile (fastPointer != null && fastPointer.next != null). The second check guarantees that fast.next exists before you try to access fast.next.next.Not handling the null/empty list case
head.next on a null referenceif (head == null) return null; as the very first line of your function. A good habit is to also consider a single-node list — the algorithm handles it correctly, but it's worth tracing through manually to confirm.Returning the wrong middle for even-length lists
fastPointer != null && fastPointer.next != null naturally returns the SECOND middle for even lists. If you need the FIRST middle, change the condition to fastPointer.next != null && fastPointer.next.next != null — this stops one step earlier.Practice These on LeetCode
Interview Questions on This Topic
Can you find the middle of a linked list in a single pass, without knowing its length? Walk me through your approach and explain WHY the fast/slow pointer technique guarantees the pointer lands exactly in the middle.
What happens to your algorithm when the linked list has an even number of nodes — say, 4 or 6 nodes? Which middle node does it return, and how would you modify it to return the other one?
while (fast.next != null && fast.next.next != null). This stops one iteration earlier — slow stays at the first middle node when the loop ends.The fast/slow pointer pattern you used here — where else can you apply it? Can you think of another linked list problem that uses the same two-pointer idea?
Frequently Asked Questions
Use the fast and slow pointer technique. Start both pointers at the head. Move the slow pointer one step at a time and the fast pointer two steps at a time. When the fast pointer reaches the end of the list, the slow pointer is sitting exactly at the middle. This works in a single pass and requires no prior knowledge of the list's length.
Time complexity is O(n) — you traverse the list once (the fast pointer visits roughly n/2 nodes twice, which simplifies to O(n)). Space complexity is O(1) — you only use two pointer variables regardless of how long the list is. No extra data structures are created.
Linked lists don't support index-based random access. In an array, elements are stored contiguously in memory, so jumping to position 5 is instant. In a linked list, each node only knows the address of the next node. To reach position 5, you must follow the chain from the head through nodes 1, 2, 3, 4, and then 5 — there's no shortcut. This is why we need pointer-based traversal techniques instead.
Fast moves twice as fast as slow. When fast reaches the end (n steps), slow has moved n/2 steps — exactly the middle. For even-length lists, the exact middle depends on the loop termination condition.
Merge sort on a linked list uses find-middle to split the list, then recursively sorts and merges. Palindrome checking on a linked list finds the middle, reverses the second half, then compares the two halves.
No. If there's a cycle, the fast and slow pointers will never reach null — they'll loop forever. You must first detect the cycle using Floyd's algorithm (also fast/slow, but with different termination). After breaking the cycle or working on the non-cyclic portion, you can find the middle. In practice, you'd check for a cycle before running the find-middle algorithm.
20+ years shipping performance-critical code where algorithms decide the bill. Notes here come from systems that actually shipped.
That's Linked List. Mark it forged?
8 min read · try the examples if you haven't