Start learning free

LESSON 150 OF 314 · LINKED LISTS

Intersection of Two Linked Lists in Go: O(m + n) Time, O(1) Space

Practice O(n) Read the full explanation

You get headA and headB, the heads of two singly linked lists. Return the first node that both lists share, or nil if they never join.

Two lists join at a node when both go on through the very same nodes, to the end.

In this picture, A has two nodes of its own and B has three. Then both go through c1, c2 and c3. The answer is c1:

A B a1 a2 b1 b2 b3 c1 c2 c3

What matters is the node itself, not its value. Two different nodes can hold the same number.

Neither list has a loop. Both lists must stay exactly as they were after your function returns.

The examples write out each list in full. They also give shared, the number of nodes at the end that are in both lists. It is 0 when the lists don't join.

The tests use shared to build the lists. Your function gets only headA and headB.

 

Example 1:

A B 3 5 5 9 4

Input: listA = [3,5,9,4], listB = [5,9,4], shared = 2
Output: the node with value 9
Explanation: The green nodes, 9 -> 4, are in both lists.
Each list also has a 5, but they are two different nodes.
So the first shared node is the 9.

Example 2:

A B 8 2 6 1 7 3

Input: listA = [8,3], listB = [2,6,1,7,3], shared = 1
Output: the node with value 3
Explanation: Only the last node, the green 3, is shared.
A has 1 node before it, and B has 4.

Example 3:

A B 6 2 8 4 1

Input: listA = [6,2,8], listB = [4,1], shared = 0
Output: nil
Explanation: No node is in both lists.

 

Constraints:

  • Each list has 1 to 30,000 nodes.
  • Each list has at least one node of its own, before the shared part.
  • Each value is from 1 to 100,000.

 

Follow up: Can you find the node in O(m + n) time and O(1) extra memory? Here m and n are the two lengths.
STEP 1 OF 17
    Ctrl + Enter to check

    GO · LINKED LISTS

    Intersection Of Two Linked Lists in Go, explained

    Problem overview

    The function getIntersectionNode receives the heads of two singly linked lists, headA and headB. The lists may merge into a shared tail: from some node on, both lists continue through the very same nodes. The function must return that first shared node, or nil if the lists never meet. The lists have no cycles, and they must be left unchanged.

    In the first example, list A is 4, 1, 8, 4, 5 and list B is 5, 6, 1, 8, 4, 5. They share the tail 8, 4, 5, so the answer is the node holding 8. It is the same node, not just a node with an equal value: A reaches it after 2 nodes and B after 3. The second 1 in B is a different node from the 1 in A, even though the values match.

    The follow-up asks for O(m + n) time and O(1) memory, where m and n are the lengths.

    Approach: align the starting points

    Two easy solutions exist. Comparing every node of A with every node of B takes O(m * n). Storing the nodes of A in a set and then checking each node of B takes O(m + n) time but O(m) memory. This lesson reaches O(1) memory with a different idea.

    If the lists intersect, everything after the meeting point is shared, so that part has the same length in both lists. The two lists can only differ in length before the meeting point. If the longer list's pointer first skips its extra nodes, both pointers are then the same distance from the end. Moving them forward together, one step at a time, makes them arrive at the intersection at the same moment. If the lists never meet, both pointers reach nil together, and nil == nil ends the loop.

    On the first example, sizeA is 5 and sizeB is 6, so B's pointer skips 1 node and lands on B's second node (the value 6). Both pointers then have 4 nodes to go. They step together: 4 and 6, then 1 and 1 (two different nodes), then 8 and 8, the same node. The loop stops and the function returns it.

    Step by step

    1. Compute both lengths with a helper: sizeA := size(headA) and sizeB := size(headB). The helper is written afterwards.
    2. Create walking pointers nA := headA and nB := headB, so the parameters stay untouched.
    3. Find the longer list. If sizeA < sizeB, list B is longer.
    4. Skip B's extra nodes: nB = moveNode(nB, sizeB-sizeA). The helper returns the node it stopped on, so the result must be assigned back. The function receives a copy of the pointer, which it can't change in the caller.
    5. Otherwise A is at least as long, and nA = moveNode(nA, sizeA-sizeB) skips A's extra nodes. When the lengths are equal, the distance is 0 and nothing moves.
    6. Loop while nA != nB. Go has no while, so this is a for with only a condition. Comparing pointers with != asks whether they refer to different nodes, not whether the values differ.
    7. Advance both with nA = nA.Next and nB = nB.Next.
    8. Return nA. It is the intersection node, or nil when the lists don't meet.
    9. Write size: a counter starts at 0, and a for head != nil loop increments it and advances head. The parameter is a copy, so walking it doesn't disturb the caller.
    10. Write moveNode: while k > 0, step node = node.Next and decrement k, then return node.

    The solution in Go

    func getIntersectionNode(headA, headB *ListNode) *ListNode {
    	sizeA := size(headA)
    	sizeB := size(headB)
    	nA := headA
    	nB := headB
    	if sizeA < sizeB {
    		nB = moveNode(nB, sizeB-sizeA)
    	} else {
    		nA = moveNode(nA, sizeA-sizeB)
    	}
    	for nA != nB {
    		nA = nA.Next
    		nB = nB.Next
    	}
    	return nA
    }
    func size(head *ListNode) int {
    	count := 0
    	for head != nil {
    		count++
    		head = head.Next
    	}
    	return count
    }
    func moveNode(node *ListNode, k int) *ListNode {
    	for k > 0 {
    		node = node.Next
    		k--
    	}
    	return node
    }
    

    Go details worth knowing:

    • Pointer comparison is identity. For two *ListNode values, == is true only if both hold the same address. Two separate nodes with equal Val are not equal as pointers. That is exactly the question this problem asks.
    • Parameters are copies. In size, the line head = head.Next changes only the local copy of the pointer. In moveNode, the same is true of node and k, which is why the caller must use the return value.
    • Helpers can follow their callers. Go resolves package-level functions regardless of the order of declaration, so size and moveNode may be written below the function that calls them.

    Complexity

    Time: O(m + n). size walks each list once, moveNode walks at most the longer list once, and the final loop takes at most as many steps as the shorter list has nodes.

    Space: O(1). Four pointers and a few integers, regardless of the list sizes. Nothing is allocated, and the lists are not modified.

    Edge cases and common mistakes

    • No intersection. For [2, 6, 4] and [1, 5] the pointers walk together until both are nil, the loop condition becomes false, and the function returns nil. The loop never dereferences a nil pointer, because both pointers become nil on the same step.
    • Equal values, different nodes. Two separately built lists [1, 2] and [1, 2] don't intersect, and the function returns nil. Comparing .Val instead of the pointers would wrongly report an intersection.
    • Intersection at the head. If both heads are the same node, the lengths are equal, nothing is skipped, and the loop doesn't run: it returns headA immediately.
    • Skipping the wrong amount. If the pointers start at different distances from the end, they never meet at the shared node. One of them reaches nil first, and the next nA.Next or nB.Next panics with a nil pointer dereference.
    • Modifying the lists. The statement requires the original structure to remain. This solution only reads Next.

    Testing

    • A = 4, 1, 8, 4, 5 and B = 5, 6, 1, 8, 4, 5, sharing the tail that starts at 8: returns the shared node with value 8.
    • A = [2, 6, 4] and B = [1, 5], no shared nodes: returns nil.
    • The same list passed as both arguments: returns that list's head.
    • Two separately built lists with equal values: returns nil.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free