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

Find the node where two singly linked lists merge.

1What it means for two linked lists to merge

Think of two footpaths in a park that start at different gates and join at a fountain, after which there is only one path to the cafe. Two walkers who start at different gates share every step after the fountain. Drawn from above, the paths look like the letter Y, and the fountain is the point where they meet.

A singly linked list is such a path. Each stop on it is a node, holding a value and one reference called next, which names the node after it; the last node's next is null, meaning nothing. Two lists merge when they share a node, the very same object in memory. Since a node has only one next, everything after a shared node is shared too, which is why the shape is a Y. We must return the first shared node, or null if there is none.

Our example has list A, 4, 1, 8, 4, 5, and list B, 5, 6, 1, 8, 4, 5. The last three nodes, 8, 4 and 5, are the same three nodes in both lists, so the answer is the node holding 8. Look carefully at the node holding 1 just before the merge in each list: those are two different nodes that happen to hold the same value. They are not the merge, and they are the trap of this problem.

This question is smaller than it looks. We will not need any extra memory or any change to the lists, only two references walking in step, and the whole idea fits in one sentence once we have seen it.

PictureTwo lists sharing a tail

Row one is list A and row two is list B. The marked nodes at the end of each row are the same three nodes, drawn twice.

  1. 41845561845
    a (A's own)
    2 nodes
    b (B's own)
    3 nodes
    c (shared)
    3 nodes

    1The last three nodes of each row are the same objects in memory. The merge point is the shared 8. The two nodes holding 1 are different nodes.

What to assume

Do we compare nodes or values?
Nodes, with ==, because equal values in separate nodes are not a merge.
What if the lists never meet?
Return null.
Can a list contain a loop?
We assume not.
May we change the lists, say by marking nodes?
No.
Can a head be null?
Yes, and then there is no merge.

Let us first do it by hand. Walk A and write down each node as you pass it: A's 4, A's 1, the shared 8, the shared 4, the shared 5. Then walk B and stop at the first node already on your list. B's 5 is not on it, nor is B's 6, nor B's own 1, but the 8 is, so 8 is the merge node. That works, and it is the HashSet idea, but it needed a whole written list.

A careful candidate asks a few questions first. Do we compare nodes or values? Nodes, with ==, because equal values in separate nodes are not a merge. What if the lists never meet? Return null. Can a list contain a loop? We assume not; the FAQs cover it. May we change the lists, say by marking nodes? No. Can a head be null? Yes, and then there is no merge.

2How to recognise this kind of linked list problem

A beginner reading this question for the first time often compares values and stops at the first equal pair, which here would wrongly stop at the two 1s. Others write two nested loops, or think of reversing both lists so the shared part comes first, which changes the lists we were told to leave alone. Each attempt teaches us something about what the question really asks.

Let us take the clues one at a time. The first is the word singly. Each node knows only the node after it, so we can only walk forwards from the heads. We cannot start at the shared end and walk back to the fountain, however tempting that is on paper.

The second clue is the phrase the node where they merge. It describes a meeting point, a node that a walk along A and a walk along B both reach. If both lists had the same number of nodes before the merge, two references moving one step at a time from the two heads would land on the merge node together. Ours do not: A has 2 nodes before the merge and B has 3. So the real problem is to cancel the difference in distance.

Together the clues point to the linked lists pattern, and to a technique called two pointers walking in step. A pointer here is a reference variable resting on one node, like a finger on a stop of the path. The rule to carry to new problems: when two walks must meet at a shared node, make them travel the same distance, either by giving one a head start equal to the difference, or by letting each walk the other's path as well.

Some patterns look similar but solve something else. Fast and slow pointers move at different speeds to find a loop or the middle of a list; here both move at the same speed and only their starting distances differ. Hashing works, as our paper list showed, but costs memory the two pointers do not need.

3The brute force, comparing every pair of nodes

The brute force is the honest first attempt: take each node of A in order and walk the whole of B looking for that same node object. It needs no insight, it is plainly correct, and it gives us a cost to beat.

Java
static Node mergeBrute(Node headA, Node headB) {
for (Node p = headA; p != null; p = p.next) // every node of A ...
for (Node q = headB; q != null; q = q.next) // ... against every node of B
if (p == q) return p; // the same node, not the same value
return null;
}

Let us count on the example. For A's 4 we walk all six nodes of B and find no match. For A's 1 we walk all six again; B's 1 has the same value, but p == q compares objects, so it is not a match. For the shared 8 we read B's 5, 6, 1 and then the 8, a match after four. That is 6 + 6 + 4 = 16 comparisons, and because we try A's nodes from the head, the first match is the merge node.

For lists of m and n nodes it may walk all of B for each node of A, so the time is O(m * n), read as order m times n, and the space is O(1), because it keeps only p and q. Two lists of 100,000 nodes could take ten billion comparisons.

There are two middle steps. Putting every node of A into a HashSet and then walking B until a node is already in the set takes O(m + n) time but O(m) memory. Counting both lengths, moving the longer list's pointer ahead by the difference, and walking both together takes O(m + n) time and O(1) memory, and it is a perfectly good answer.

Where was the waste in the brute force? It walked B again for every node of A. All we really needed was to line the two pointers up at the same distance from the merge, and the length trick shows that this is enough.

4Getting to the switching pointers, one observation at a time

Let us name the three parts of the Y. A's own part, before the merge, has a nodes, B's own part has b nodes, and the shared tail has c nodes. In our example a is 2, b is 3 and c is 3. The tails are identical, so the only thing keeping two in-step pointers apart is that a and b differ, by one node here.

The length trick counts first. A has 5 nodes and B has 6, so B's pointer takes one step first, onto B's 6. Then both walk together: B's 6 beside A's 4, B's 1 beside A's 1, two different nodes, and then the shared 8 beside the shared 8, where they meet. It works, but it walks each list once just to count.

Now the beginner's question at the hard step: can we line the pointers up without counting anything? Go back to the park. Suppose each walker, on reaching the cafe, goes straight to the other walker's gate and walks that path too. The first walker covers her own path, then the other one up to the fountain; the second does the same in the other order. They walk the same total distance, so they reach the fountain together on the second lap.

Step throughWhy switching lists makes the distances equal

Left bar: pointer p, which starts on A. Right bar: pointer q, which starts on B. The upper part of each bar is the second lap.

  1. 23
    p to merge
    a = 2
    q to merge
    b = 3

    1Walking only its own list, p is 2 steps from the merge and q is 3 steps from it, so in step they arrive at different times.

  2. 23
    p total
    a + c + 1 + b = 9
    q total
    b + c + 1 + a = 9

    2If each also walks the other's list, p walks 2, then 3 + 1 + 3 = 7 more, and q walks 3, then 3 + 1 + 2 = 6 more. Both totals are 9.

In list terms, p starts at A's head and q at B's head, and both take one step at a time. When p walks off the end of A it becomes null, and on its next step it jumps to B's head; q does the same in the other direction. Count p's steps to the merge on its second lap: a + c to walk off A, one to jump, and b along B, a + c + 1 + b. For q it is b + c + 1 + a. The totals are the same, here 9 each, so both stand on the merge node after the same number of steps.

Why do they not stop too early? The loop stops only when p == q. Before the second lap they sit at different distances from the merge, so even in the shared tail they are on different nodes. And if the lists never merge, c is 0, and both reach null at the same moment after a + b + 1 steps; null == null, so the loop stops and returns null, which is the right answer.

The mistake beginners make most often is the jump. It is tempting to write the jump as if p.next is null then p = headB, skipping the null step. Then the pointers are never null at the same time, and when the lists do not merge the loop runs forever. The version that lets p become null first, and jumps on the next step, handles both cases with one condition.

5Watching the two pointers meet

The picture runs mergeNode on our two lists, one loop step per frame, with positions taken from a run of the Java code. Row one is A and row two is B, and the shared tail is drawn in both rows, so a pointer on a shared node appears in the row of the list it is walking. When a pointer is null it is not drawn, and the panel says so.

Step throughSwitching pointers on A = 4 1 8 4 5 and B = 5 6 1 8 4 5

Row one is A, row two is B. p is the marked node walking from A first, q the one walking from B first.

  1. 41845p561845q
    steps
    0
    p is on
    A's 4
    q is on
    B's 5
    p == q
    no

    1p starts on the head of A and q on the head of B. They are on different nodes, so the loop begins.

  2. 41845p561845q
    steps
    1
    p is on
    A's 1
    q is on
    B's 6
    p == q
    no

    2Both step once. p is on A's 1 and q on B's 6, still different nodes.

  3. 41845p561845q
    steps
    2
    p is on
    shared 8
    q is on
    B's 1
    p == q
    no

    3p reaches the shared 8 first, because A's own part is one node shorter, while q is still on B's 1.

  4. 41845p561845q
    steps
    3
    p is on
    shared 4
    q is on
    shared 8
    p == q
    no

    4p moves to the shared 4 and q reaches the shared 8. Both are in the shared tail, but one node apart, so they never line up on this lap.

  5. 41845p561845q
    steps
    4
    p is on
    shared 5
    q is on
    shared 4
    p == q
    no

    5p is on the shared 5, the last node, and q is on the shared 4.

  6. 41845561845q
    steps
    5
    p is on
    null
    q is on
    shared 5
    p == q
    no

    6p steps off the end of A and becomes null. q is on the shared 5. A null p is not equal to q, so we go on.

  7. 41845561845p
    steps
    6
    p is on
    B's 5
    q is on
    null
    p == q
    no

    7Because p was null, it jumps to the head of B. q steps off the end and becomes null.

  8. 41845q561845p
    steps
    7
    p is on
    B's 6
    q is on
    A's 4
    p == q
    no

    8Because q was null, it jumps to the head of A. p is on B's 6. Each pointer is now walking the other list.

  9. 41845q561845p
    steps
    8
    p is on
    B's 1
    q is on
    A's 1
    p == q
    no

    9p is on B's 1 and q on A's 1. Their values are equal, but they are different nodes, so p != q and the loop goes on.

  10. 41845q561845p
    steps
    9
    p is on
    shared 8
    q is on
    shared 8
    p == q
    yes

    10Both step onto the shared 8 at the same moment. p == q, the loop stops, and we return this node, the merge point.

Let us read the important frames back. On the first lap p reaches the shared 8 at step 2 and q reaches it at step 3, one step apart, so they never line up. That is the difference between a = 2 and b = 3.

Steps 5 to 7 are the switch. p becomes null at step 5 and jumps to B's head at step 6, while q becomes null at step 6 and jumps to A's head at step 7. At step 7, p is on B's 6 and q on A's 4, and each is exactly two steps from the merge.

Step 8 is the trap made visible: p is on B's 1 and q on A's 1, equal values but different nodes, and the loop correctly goes on. At step 9 both reach the shared 8 together, and we return it.

6The complete Java program

Here is the full program. The helpers build the lists so that the last own node of each points to the same shared chain, which is how a merge happens in memory. main tests our example, two separate lists, two identical heads and a null head, and each comment shows what that line prints.

Java
public class MergePoint {
static class Node {
int val; Node next;
Node(int val) { this.val = val; }
}
// Each pointer walks its own list, then the other one. Both travel a + c + 1 + b steps,
// so they arrive at the merge node together, or at null together.
static Node mergeNode(Node headA, Node headB) {
Node p = headA, q = headB;
while (p != q) { // compares references, not values
p = (p == null) ? headB : p.next;
q = (q == null) ? headA : q.next;
}
return p;
}
static Node chain(int... vals) {
Node dummy = new Node(0), tail = dummy;
for (int v : vals) { tail.next = new Node(v); tail = tail.next; }
return dummy.next;
}
static Node last(Node n) {
while (n.next != null) n = n.next;
return n;
}
public static void main(String[] args) {
Node shared = chain(8, 4, 5);
Node a = chain(4, 1); last(a).next = shared; // 4 1 8 4 5
Node b = chain(5, 6, 1); last(b).next = shared; // 5 6 1 8 4 5
Node m = mergeNode(a, b);
System.out.println(m.val + " " + (m == shared)); // 8 true
System.out.println(mergeNode(chain(1, 2, 3), chain(4, 5))); // null
System.out.println(mergeNode(shared, shared).val); // 8
System.out.println(mergeNode(null, shared)); // null
}
}

Let us read mergeNode in the order it runs. p starts on A's head and q on B's head. The while condition p != q compares references, so it asks whether the two pointers rest on the same node object, never whether two values are equal.

Inside the loop each pointer takes one step. If p is null it jumps to headB, and otherwise it moves to p.next; q does the same with headA. Both move in every pass, which keeps them in step. When the loop ends, p and q are equal, either on the merge node or both null, so returning p covers both cases.

In main, chain builds a list from values, and last finds a list's final node, so last(a).next = shared joins A's own part to the shared tail. The first line prints 8 true: the value of the returned node, and confirmation that it is the very node shared. Two lists that never meet print null. When both heads are the same node, p == q holds before any step, so we return it at once and print 8. A null head prints null, because p walks A, which is empty, then B, while q walks B and then nothing, and both become null together.

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

Let us count on the example first. The loop ran 9 times for lists of 5 and 6 nodes. In general each pointer walks at most its own list, one jump, and the other list, so the loop runs at most m + n + 1 times, and the time is O(m + n). We cannot do better, because the merge could be at the very end, and we must reach it.

The space is O(1). We keep two references, whatever the length of the lists, and we never change a node.

Comparing the approaches
ApproachTimeExtra space
Every node of A against every node of BO(m * n)O(1)
HashSet of A's nodesO(m + n)O(m)
Count lengths, give the longer a head startO(m + n)O(1)
Switching pointersO(m + n)O(1)

The edge cases need no special code. A null head makes one pointer start at null and, as main shows, the pair still ends together on null. Lists that merge at their first node, or are the same list, are found after zero steps. Lists of very different lengths are handled by the switch without any counting. Lists that never meet end with both pointers null after m + n + 1 steps.

The assumption that matters is that neither list has a loop. With a loop, a pointer might never reach null, and the while loop could run forever, so a careful candidate states that assumption aloud.

8Follow-up questions

Why must we compare nodes and not values?

Because two lists merge only when they share an actual node object, and different nodes can hold equal values, as the two nodes holding 1 in our example do. The test p == q asks Java whether both references name the same object. A test like p.val == q.val would stop at step 8 of our walk, on two unrelated nodes, and return a node that is not shared at all.

Which answer should I give in an interview, the length trick or switching pointers?

Either is a complete answer with the same O(m + n) time and O(1) space. The length trick is easier to explain from scratch: count both lists, move the longer one ahead by the difference, then walk together. Switching pointers is shorter to write and needs no counting, but it deserves the distance argument, a + c + 1 + b against b + c + 1 + a, said aloud. A good plan is to describe the length trick first and offer switching pointers as the tidier version.

How would you find the merge node if the lists may contain loops?

First check each list for a loop with Floyd's slow and fast pointers. If one list has a loop and the other does not, they cannot share a node, because the shared tail would have to contain the loop. If neither has a loop, the usual method works. If both do, they share a node only if their loops are the same, which we check by walking once around one loop looking for a node of the other. When the merge happens before the loop starts, the length trick on the parts before the loop finds it.

9Conclusion

We found the node where two singly linked lists merge with two pointers walking in step. Each walks its own list and then the other one, so both travel a + c + 1 + b steps and reach the merge node at the same moment, or reach null together if there is no merge. It takes O(m + n) time and O(1) space and compares node references, never values. The clue to remember is two walks that must meet at a shared node: make them travel the same distance, by a head start or by switching lists.

What to say in the interviewAbout 90 seconds aloud

Open with

Let me confirm: the lists merge if they share a node object, so I compare references, not values; I return null if they never meet; and I assume neither list has a cycle and that I should not modify them.

Then, if they want more

The brute force compares every node of A with every node of B, O(m times n). A HashSet of A's nodes brings that to O(m plus n) but uses O(m) memory. The real problem is that the parts before the merge have different lengths, so I need to equalise the distance two in-step pointers travel.

I could count both lengths and give the longer list a head start. Without counting, I let p walk A and then B, and q walk B and then A, jumping to the other head when a pointer becomes null.

Then p travels a plus c plus 1 plus b steps to the merge and q travels b plus c plus 1 plus a, the same, so they meet at the merge node. If there is no merge, both become null at the same time and I return null.

In the example, A is 4 1 8 4 5 and B is 5 6 1 8 4 5; after nine steps both pointers are on the shared 8. That is O(m plus n) time and O(1) space.

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 43 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 "Find the node where two singly linked lists merge.": https://scrivio-interview-library.netlify.app/data-structures/solutions/find-the-node-where-two-singly-linked-lists-merge-3a8fb4/