Problem Statement (Asked By Google)
You are given two singly linked lists that intersect at some node. Your task is to find and return the first intersecting node.
Input:
A = 3 -> 7 -> 8 -> 10
B = 99 -> 1 -> 8 -> 10 Output:
Node with value 8 Explanation:
Problem link: https://leetcode.com/problems/intersection-of-two-linked-lists/
Disclaimer: Try solving the problem on your own first! Use this solution only as a reference to learn and improve.
HashSet.HashSet.
import java.util.HashSet;
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class IntersectionLinkedList {
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> visited = new HashSet<>();
// Store all nodes of list A in the HashSet
while (headA != null) {
visited.add(headA);
headA = headA.next;
}
// Traverse list B and check if any node is already in the HashSet
while (headB != null) {
if (visited.contains(headB)) {
return headB; // Found the intersection node
}
headB = headB.next;
}
return null; // No intersection found
}
public static void main(String[] args) {
ListNode common = new ListNode(8);
common.next = new ListNode(10);
ListNode headA = new ListNode(3);
headA.next = new ListNode(6);
headA.next.next = common;
ListNode headB = new ListNode(5);
headB.next = common;
ListNode intersection = getIntersectionNode(headA, headB);
System.out.println(intersection != null ? "Intersection at: " + intersection.val : "No Intersection");
}
}
HashSet).
public class IntersectionLinkedListOptimized {
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lengthA = getLength(headA);
int lengthB = getLength(headB);
// Align the starting positions
while (lengthA > lengthB) {
headA = headA.next;
lengthA--;
}
while (lengthB > lengthA) {
headB = headB.next;
lengthB--;
}
// Traverse both lists together to find intersection
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA; // Either the intersection node or null
}
private static int getLength(ListNode head) {
int length = 0;
while (head != null) {
length++;
head = head.next;
}
return length;
}
public static void main(String[] args) {
ListNode common = new ListNode(8);
common.next = new ListNode(10);
ListNode headA = new ListNode(3);
headA.next = new ListNode(6);
headA.next.next = common;
ListNode headB = new ListNode(5);
headB.next = common;
ListNode intersection = getIntersectionNode(headA, headB);
System.out.println(intersection != null ? "Intersection at: " + intersection.val : "No Intersection");
}
}
| Approach | Time Complexity | Space Complexity | Explanation |
|---|---|---|---|
| Brute Force (HashSet) | O(M+N) | O(max(M,N)) | Uses extra space to store visited nodes. |
| Optimized (Two Pointers) | O(M+N) | O(1) | Uses length difference to align pointers efficiently. |
Did this solution help you understand the concept better? Let me know your thoughts in the comments below! Don’t forget to follow us on Instagram @coderz.py for more DSA tips and solutions to ace your coding interviews.
A design pattern is a reusable solution to a commonly occurring problem in software design. They…
Factory Method is a creational design pattern that deals with the object creation. It separates…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…