Scrivio Interview library
Linked listsLinked listsReported 80 times · 14 min read · updated 15 November 2026

Return the middle node of a singly linked list.

1What the middle of a linked list is

Picture two friends climbing a staircase together. One climbs one step at a time; the other, in a hurry, takes two steps at a time. They start together, and every second each takes one stride. When the hurried friend reaches the top, where is the other one? Exactly half way up, because they always covered half the distance. Keep this staircase in mind, because it is the whole solution.

A singly linked list is a chain of nodes. Each node holds a value and a reference called next, which tells us the node that comes after it. We are handed only the first node, called the head, and the last node's next is null, meaning there is nothing after it. To reach the fourth node we must start at the head and follow next three times; there is no way to jump there directly.

The middle node is the one with as many nodes before it as after it. Let us do it by hand. In 1 -> 2 -> 3 -> 4 -> 5, node 3 has two nodes before it, 1 and 2, and two after it, 4 and 5, so node 3 is the middle. Now take six nodes, 1 -> 2 -> 3 -> 4 -> 5 -> 6. Node 3 has two before and three after, and node 4 has three before and two after, so there are two middle nodes. The question has to choose one, and the usual choice is the second, node 4.

There is a neat way to say both answers at once. Number the nodes from 0, so node 1 is at index 0. For five nodes the middle, node 3, is at index 2, and for six nodes our chosen middle, node 4, is at index 3. Both are n / 2, using whole-number division, which throws away the remainder: 5 / 2 is 2 and 6 / 2 is 3.

PictureThe two example lists and their middle nodes

Top: five nodes, whose middle is node 3. Bottom: six nodes, where we agree to return node 4, the second of the two middle nodes.

  1. 12345123456

    1With five nodes, node 3 has two nodes on each side. With six nodes, both 3 and 4 sit in the middle, and we return node 4.

What to assume

For an even length, do we want the first or the second middle?
We return the second.
Do we return the node or its value?
The node, because a caller often wants to split the list there.
Can the list be empty?
Yes, and then we return null.
Do we know the length?
No, and the interviewer usually asks us to find the middle in a single walk.

A careful candidate settles a few things first. For an even length, do we want the first or the second middle? We will return the second and also show the small change that gives the first. Do we return the node or its value? The node, because a caller often wants to split the list there. Can the list be empty? Yes, and then we return null. Do we know the length? No, and the interviewer usually asks us to find the middle in a single walk.

2How to recognise the fast and slow pointer pattern

A beginner who meets this question usually thinks of arrays first, where the middle is simply a[n / 2]. The natural plan is to find n and then go to index n / 2. On a linked list both halves of that plan need a walk: one walk to count and a second walk to reach the middle. It works, and it is a fine starting point, but it reads every node and then reads half of them again.

Let us read the question slowly and take its clues one at a time. The first clue is the word middle. The answer is a position that depends on the length: half of it. Any time the position we want is a fraction of a length we do not know, we should think about how to measure that fraction without measuring the length first.

The second clue is singly linked list. Each node only knows the node after it, so we can only walk forward, and the list keeps no count of its nodes. Whatever we do must happen while walking forward, with pointers, which are simply variables that refer to nodes, like a finger resting on one step of the staircase.

Together these clues point to the linked lists family and to the technique called fast and slow pointers. Two pointers start at the head and walk the same list at different speeds, one node per step for slow and two nodes per step for fast. Because their speeds have a fixed ratio, their positions do too: wherever fast is, slow has gone half as far. That is the staircase again. The rule to carry to new problems is this: when a question asks for a position that is a fraction of an unknown length, let a faster pointer measure the length while a slower one walks to the fraction.

Some patterns look close but do not fit. The k-th node from the end is a fixed distance from the end, not a fraction, so it uses two pointers moving at the same speed with a gap of k between them. Binary search also goes to the middle, but it jumps there by index, which a linked list cannot do. And detecting a loop uses the same two speeds, but asks whether the pointers ever meet, not where slow is when fast stops.

3The honest first attempts, copying or counting

When a structure will not let us do what we want, a good first move is to turn it into one that will. This is our brute force, the honest first attempt anyone would make: an ArrayList can be read at any index, so we copy every node into one and return the node at index size / 2. It is correct, short and easy to check, which is exactly what a first attempt should be.

Let us try it by hand on five nodes. The walk adds nodes 1, 2, 3, 4 and 5 to the list, its size is 5, 5 / 2 is 2, and the node at index 2 is node 3. On six nodes the size is 6, 6 / 2 is 3, and the node at index 3 is node 4. Both match the answers we worked out on paper.

Java
static Node middleByCopy(Node head) {
List<Node> nodes = new ArrayList<>();
for (Node n = head; n != null; n = n.next) nodes.add(n); // one walk, keep every node
return nodes.isEmpty() ? null : nodes.get(nodes.size() / 2);
}

Let us count. The walk visits each of the n nodes once, so the time is O(n), meaning the work grows in step with n. But the ArrayList holds a reference to every node, so the extra space is O(n) as well. We stored n references and used exactly one of them; that is the waste.

A second attempt removes the memory by counting instead: walk once to count the nodes, then walk n / 2 steps from the head. For five nodes that is five visits to count and two more steps, seven in all, and for n nodes about 1.5n. The space is now O(1), constant, because we only keep a counter and one pointer. The waste has moved: the whole first walk exists only to learn one number, and the second walk then repeats the first half of it.

4Getting from two walks to one walk

Look again at the counting version. The second walk goes exactly half as far as the first. So here is the small observation that gives the whole solution: instead of doing the two walks one after the other, do them at the same time. One pointer, fast, does the long walk to the end, taking two nodes per step. Another, slow, does the short walk, taking one node per step. When fast cannot go on, slow has done half of fast's walk, which is just where the second walk would have ended.

In code, both start at the head, and while fast is not null and fast.next is not null, slow = slow.next moves slow one node and fast = fast.next.next moves fast two. We never learn n, and we do not need to; the difference in speed does the division for us.

Now the question a beginner should ask. Why does slow land on index n / 2 for both odd and even lengths? Let us check it with numbers. After k steps slow is at index k and fast at index 2k. With five nodes, fast stops on the last node, index 4, which is 2k, so k is 2, and slow is at index 2, node 3. With six nodes, fast can still hop from index 4 over index 5 to null, which is one place past the last node, index 6, so k is 3 and slow is at index 3, node 4. In both cases k comes out as n / 2.

That explains the two halves of the loop condition, and why we need both. fast == null is how a list of even length ends under fast's feet, and fast.next == null is how a list of odd length ends. If we wrote only fast.next != null, then on six nodes fast would become null and the next test would read null.next and throw a NullPointerException. Checking fast first, from left to right, prevents that.

What if the caller wants the first middle, node 3, for six nodes? We stop one step earlier, by looping only while fast.next and fast.next.next both exist. For six nodes that stops with fast on node 5 and slow on node 3. Merge sort on a linked list wants this version, because it splits the list into a front half that is never the shorter one.

5Watching slow and fast on five and six nodes

The picture first plays the loop on five nodes and then on six. Slow is the highlighted node and fast the one compared with it, with their names under them. In each frame, notice that fast is always about twice as far from the head as slow, which is the staircase in action, and watch which half of the loop test stops the walk.

Step throughSlow and fast on five nodes, then on six nodes

Slow moves one node per step and fast moves two. The first four frames use five nodes, the last five use six.

  1. 12345slowfast
    slow
    node 1
    fast
    node 1

    1Five nodes. Both pointers start on the head, node 1. The loop test asks whether fast and fast.next exist, and they do.

  2. 12345slowfast
    slow
    node 2
    fast
    node 3

    2Step 1 moves slow one node, to node 2, and fast two nodes, to node 3. Fast has walked twice as far as slow.

  3. 12345slowfast
    slow
    node 3
    fast
    node 5

    3Step 2 moves slow to node 3 and fast to node 5, the last node. Fast is still exactly twice as far from the head as slow.

  4. 12345slowfast
    slow
    node 3
    fast
    node 5
    returns
    node 3

    4Fast is on node 5, whose next is null, so the test fast.next != null fails and the loop stops. Slow is on node 3, the middle, and we return it.

  5. 123456slowfast
    slow
    node 1
    fast
    node 1

    5Six nodes now. Both pointers start on node 1 again, and the loop test passes.

  6. 123456slowfast
    slow
    node 2
    fast
    node 3

    6Step 1 moves slow to node 2 and fast to node 3, exactly as before.

  7. 123456slowfast
    slow
    node 3
    fast
    node 5

    7Step 2 moves slow to node 3 and fast to node 5. This time node 5 has a next, node 6, so the loop test still passes.

  8. 123456slow
    slow
    node 4
    fast
    null, past the end

    8Step 3 moves slow to node 4, while fast goes 5 -> 6 -> null and steps off the end of the list.

  9. 123456slow
    slow
    node 4
    fast
    null, past the end
    returns
    node 4

    9Now fast is null, so the test fast != null fails and the loop stops. Slow is on node 4, the second middle, and we return it.

Let us read the important frames back. In the third frame, five nodes, fast has reached node 5, the last node. The test fast != null passes, but fast.next != null fails, so the loop stops with slow on node 3, the true middle.

With six nodes the first two steps are identical, but this time node 5 has a next, so fast takes one more hop, from node 5 over node 6 to null, and slow moves on to node 4. Now it is the first half of the test, fast != null, that stops the loop. The two lists differ only in which half of the test ends the walk, and that small difference is exactly what picks the second middle for even lengths.

6The complete Java program

Here is the full program. It has a small Node class, the middle method that returns the second middle, the firstMiddle variant, and a build helper that makes a list 1 -> 2 -> ... -> n. The main method tries five nodes, six nodes with both conventions, one node and an empty list, and the comment on each line shows what it prints.

Java
public class MiddleNode {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
// Slow moves one step, fast moves two; when fast runs out, slow is in the middle.
// For an even length this returns the second of the two middle nodes.
static Node middle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// Variant: the first of the two middle nodes for an even length.
static Node firstMiddle(Node head) {
if (head == null) return null;
Node slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
static Node build(int n) {
Node dummy = new Node(0), tail = dummy;
for (int i = 1; i <= n; i++) { tail.next = new Node(i); tail = tail.next; }
return dummy.next;
}
public static void main(String[] args) {
System.out.println(middle(build(5)).val); // 3
System.out.println(middle(build(6)).val); // 4
System.out.println(firstMiddle(build(6)).val); // 3
System.out.println(middle(build(1)).val); // 1
System.out.println(middle(null)); // null
}
}

Let us read middle in the order it runs. slow and fast both start on head. If head is null, both are null, the loop test fails at once, and we return null, which is right for an empty list.

The while condition reads fast != null first and fast.next != null second. Java stops at the first false part, so we never look at the next of a null. Inside the loop, slow = slow.next needs no check of its own, because slow is always behind fast and fast has just shown the nodes ahead exist. Then fast = fast.next.next takes the two hops. When the loop ends, slow is on index n / 2, and we return it.

firstMiddle differs in two places. Its loop asks whether fast.next and fast.next.next exist, so fast stops one step sooner on even lengths. And because that test reads fast.next straight away, it must first check that head is not null, which is the line at the top. The build helper uses a dummy node in front so the first real node needs no special case. So main prints 3, 4, 3, 1 and null.

7How much it costs, and the cases at the edges

Let us count on the example first. With six nodes the loop ran three times, and each step did two reference moves, so about three small units of work. For n nodes the loop runs about n / 2 times, so the time is O(n). Counted in node visits, slow visits about n / 2 nodes and fast about n, so roughly n visits in all, against 1.5n for the count-then-walk version.

The space is the same however long the list is, two references, so it is O(1). The copying version kept one reference per node, O(n).

Comparing the ways to find the middle
ApproachWalksTimeExtra space
Copy nodes into an ArrayList1O(n)O(n)
Count, then walk n / 22O(n)O(1)
Slow and fast pointers1O(n)O(1)

The edge cases need no special code in middle. An empty list returns null, as we saw. A single node has fast.next null, so the loop never runs and the node itself is returned, which is right, since it is its own middle. A two-node list, 1 -> 2, takes one step: slow moves to node 2 and fast to null, so middle returns node 2, while firstMiddle does not loop at all and returns node 1. That two-node case is a good one to trace by hand if you ever doubt which middle your loop condition gives.

8Follow-up questions

How do you find the k-th node from the end in one pass?

This time the target is a fixed distance from the end rather than a fraction, so we keep a fixed gap instead of different speeds. First move a lead pointer k nodes ahead. Then move the lead and a second pointer together, one node per step. When the lead reaches null, the second pointer is k nodes from the end. On 1 -> 2 -> 3 -> 4 -> 5 with k = 2, the lead starts on node 3, and when it falls off the end the other pointer is on node 4. If the lead hits null before taking k steps, the list is shorter than k.

Where is finding the middle used inside bigger problems?

Merge sort on a linked list finds the first middle, cuts the list there, sorts both halves and merges them, giving O(n log n) time without any indexing. Checking whether a list is a palindrome finds the middle, reverses the second half in place, and compares the two halves node by node. Reordering a list as first, last, second, second to last uses the same three steps: find the middle, reverse the second half, and weave the halves together.

9Conclusion

We found the middle of a singly linked list without knowing its length. Copying the nodes costs O(n) memory and counting first costs a second walk, but sending slow one node per step and fast two nodes per step puts slow on index n / 2 exactly when fast runs out, in one walk with O(1) space. For an even length that is the second middle, and looping while fast.next and fast.next.next exist gives the first. The clue to remember is a position that is a fraction of an unknown length: let a fast pointer measure the length while a slow one walks to the fraction.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would agree on the convention: for an even length I return the second middle, the node at index n over two, and an empty list returns null.

Then, if they want more

A simple approach copies every node into an ArrayList and returns the one at size over two, which is O(n) time and O(n) space. Counting first and then walking half way is constant space, but it walks the list one and a half times.

Since I need a position that is a fraction of a length I do not know, I would use fast and slow pointers. Both start at the head. While fast and fast.next are not null, slow moves one node and fast moves two, so slow always covers half of fast's distance.

For five nodes fast stops on the last node and slow is on node three; for six, fast steps past the end to null and slow is on node four, the second middle. Checking fast before fast.next avoids a null pointer exception. For the first middle I loop while fast.next and fast.next.next exist.

It is one pass, O(n) time and O(1) space, and an empty list or a single node need no special case.

About this answer

Last updated
15 November 2026
Written for
Java 21, using only java.util
Checked
The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
Where the question comes from
Public interview write-ups, reworded by Scrivio; counted 80 times in that collection. How it is counted
Official documentation
java.util, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Return the middle node of a singly linked list.": https://scrivio-interview-library.netlify.app/data-structures/solutions/return-the-middle-node-of-a-singly-linked-list-c7b6ae/