Intersection of Two Linked Lists - Complete Solution Guide
Intersection of Two Linked Lists is LeetCode problem 160, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the heads of two singly linked-lists headA and headB , return the node at which the two lists intersect . If the two linked lists have no intersection at all, return null . For example, the following two linked lists begin to intersect at node c1 : The test cases are generated such that there are no cycles anywhere in the entire linked structure. Note that the linked lists must retain their original structure after the function returns. Custom Judge: The inputs to the judge are given as fo
Detailed Explanation
The problem asks to find the node at which two singly linked lists intersect. The input consists of the heads of two linked lists, `headA` and `headB`. The output is the intersecting node (if it exists); otherwise, it's `null` (or equivalent in the respective language). The lists might intersect at any point, or not at all. Importantly, the lists' structure must remain unchanged after the function execution. The problem also provides example inputs and outputs to illustrate the concept of intersection, highlighting that the same numerical value doesn't automatically mean intersection; it's about the memory addresses of the nodes.
Solution Approach
The solution uses a two-pointer approach. Two pointers, `a_pointer` and `b_pointer`, are initialized to point to the heads of `headA` and `headB`, respectively. The pointers traverse the lists simultaneously. If `a_pointer` reaches the end of list A (`a_pointer is None`), it's reset to `headB`; similarly, `b_pointer` is reset to `headA` if it reaches the end of list B. This process continues until the two pointers meet. If they meet at a node, that's the intersection node; if they both reach the ends (become `null` simultaneously), there is no intersection.
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `a_pointer` to `headA` and `b_pointer` to `headB`. Check if either head is null. If so, return null.
- Step 2: Enter a `while` loop that continues as long as `a_pointer` and `b_pointer` are not equal.
- Step 3: Inside the loop: If `a_pointer` is `null`, set it to `headB`; otherwise, move `a_pointer` to its next node (`a_pointer.next`).
- Step 4: Similarly, if `b_pointer` is `null`, set it to `headA`; otherwise, move `b_pointer` to its next node (`b_pointer.next`).
- Step 5: After the loop, `a_pointer` (and `b_pointer`) will point to the intersection node (or `null` if there's no intersection). Return `a_pointer`.
Key Insights
- Insight 1: The key observation is that if we traverse both lists simultaneously, one pointer moving one step at a time, if there's an intersection, the pointers will eventually meet at the intersection point. If there's no intersection, the pointers will reach the end of both lists simultaneously (becoming `null`).
- Insight 2: The Two Pointers approach is perfectly suited here. It cleverly avoids the need for extra space (like a hash table) by cleverly manipulating the traversal of the two lists.
- Insight 3: Handling `null` pointers is crucial. The code must gracefully handle cases where one or both lists are empty, or the intersection is at the end of one of the lists.
Complexity Analysis
Time Complexity: O(m+n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, Linked List, Two Pointers.
Companies
Asked at: Accenture, Airbnb, Goldman Sachs, Nvidia, Oracle, TikTok, VMware.