Java Linked List Program · OT13
Find the Intersection Point of Two Linked Lists in Java
Two lists can share their tail: from some node onwards, they are the same nodes. Walk both lists, switching to the other list at the end, and the pointers meet at that node.
Problem Statement
Given the heads of two singly linked lists, return the first node that belongs to both lists, or null if they never join.
Nodes are compared by identity, not by value: two different nodes with the same value do not count. The lists must not be changed.
Input/Output Examples
10→20→[30→40] and 15→[30→40]Output30Explanation: both lists continue through the same node 30.10→20→30→40 and 1→2Outputno intersectionExplanation: the lists share no node.30→40 and 15→30→40Output30Explanation: the first list starts at the shared node itself.Visual Explanation
- Pointer ppath102030401530A, then switch to B
- Pointer qpath153040102030B, then switch to A
- Meetboth took 5 stepsnode 30
If the lists have different lengths, switching lists cancels out the difference. If they never intersect, both pointers reach null at the same time, and the loop ends.
Approaches
The switching trick uses no extra memory. The set-based approach is easier to explain but stores one list.
Approach 1: Switching Two Pointers (Recommended)
Idea: advance both pointers; when one reaches the end, restart it at the head of the other list; they meet at the intersection or at null.
- Set
p = headAandq = headB. - While
p != q, move each one step, jumping to the other head at the end. - Return
p.
Time: O(n + m) · Space: O(1).
Approach 2: HashSet of Nodes
Idea: store every node of the first list, then return the first node of the second list that is in the set.
- Add every node of list A to a
HashSet. - Walk list B and return the first node found in the set.
- Return
nullif none is found.
Time: O(n + m) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Switching Two Pointers (Recommended)
Each pointer travels at most n + m steps, so the loop always ends.
public class IntersectionSwitch {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
// Each pointer walks its own list and then the other one; both travel lenA + lenB steps
static Node intersection(Node a, Node b) {
Node p = a;
Node q = b;
while (p != q) {
p = (p == null) ? b : p.next;
q = (q == null) ? a : q.next;
}
return p; // the shared node, or null
}
public static void main(String[] args) {
Node shared = new Node(30);
shared.next = new Node(40);
Node a = new Node(10);
a.next = new Node(20);
a.next.next = shared; // 10 -> 20 -> 30 -> 40
Node b = new Node(15);
b.next = shared; // 15 -> 30 -> 40
Node c = new Node(1);
c.next = new Node(2); // 1 -> 2, no shared node
print(intersection(a, b));
print(intersection(a, c));
print(intersection(shared, b));
}
static void print(Node n) {
System.out.println(n == null ? "no intersection" : "intersect at " + n.val);
}
}
intersect at 30 no intersection intersect at 30
Time complexity: O(n + m) · Space complexity: O(1).
Approach 2: HashSet of Nodes
Because Node does not override equals(), the set compares nodes by identity, which is exactly what is needed.
import java.util.HashSet;
import java.util.Set;
public class IntersectionSet {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
// Remembers every node of list A (by identity), then finds the first node of B in the set
static Node intersection(Node a, Node b) {
Set<Node> seen = new HashSet<>();
for (Node n = a; n != null; n = n.next) {
seen.add(n);
}
for (Node n = b; n != null; n = n.next) {
if (seen.contains(n)) {
return n;
}
}
return null;
}
public static void main(String[] args) {
Node shared = new Node(30);
shared.next = new Node(40);
Node a = new Node(10);
a.next = new Node(20);
a.next.next = shared;
Node b = new Node(15);
b.next = shared;
Node c = new Node(1);
c.next = new Node(2);
print(intersection(a, b));
print(intersection(a, c));
print(intersection(shared, b));
}
static void print(Node n) {
System.out.println(n == null ? "no intersection" : "intersect at " + n.val);
}
}
intersect at 30 no intersection intersect at 30
Time complexity: O(n + m) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Switching Two Pointers | O(n + m) | O(1) |
| HashSet of Nodes | O(n + m) | O(n) |
Here n and m are the lengths of the lists. Both approaches visit each node a constant number of times; only the set approach needs memory for one list.
Interview Tips and Recommended Approach
- Compare nodes, not values. Two different nodes can hold the same number.
- Explain why the pointers meet. Both travel
a + c + bsteps, wherecis the shared part. - Know the length-difference method. Advancing the longer list by the length difference first also works in
O(1)space. - Do not modify the lists. Tricks that mark or relink nodes break the input.
Recommended Approach: Switching Two Pointers
Why: it uses constant extra memory, needs no length calculation and is only a few lines long.