This is one of those problems where the clean idea matters more than the amount of code. For 160. Intersection of Two Linked Lists, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(m+n), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 52 ms, faster than 98.21% of C++ online submissions for Intersection of Two Linked Lists.
02//Memory Usage: 19.1 MB, less than 14.04% of C++ online submissions for Intersection of Two Linked Lists.
03
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 * int val;
08 * ListNode *next;
09 * ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
15 vector<ListNode*> lA, lB;
16 ListNode *curA = headA, *curB = headB;
17
18 while(curA){
19 lA.push_back(curA);
20 curA = curA->next;
21 }
22
23 while(curB){
24 lB.push_back(curB);
25 curB = curB->next;
26 }
27
28 int i = 0;
29 while(i < min(lA.size(), lB.size())){
30 if(lA[lA.size()-1-i] != lB[lB.size()-1-i]){
31 break;
32 }
33 i++;
34 }
35
36 if(i == 0) return NULL;
37 return lA[lA.size()-1-(i-1)];
38 }
39};
40
41//https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/49785/Java-solution-without-knowing-the-difference-in-len!
42//Two pointers
43//time: O(m+n), space: O(1)
44//Runtime: 52 ms, faster than 98.21% of C++ online submissions for Intersection of Two Linked Lists.
45//Memory Usage: 16.8 MB, less than 41.28% of C++ online submissions for Intersection of Two Linked Lists.
46
47class Solution {
48public:
49 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
50 if(headA == NULL || headB == NULL) return NULL;
51
52 ListNode *pA = headA, *pB = headB;
53
54 //if length of A and B are different, we will stop at 2nd iteration
55 while(pA != pB){
56 //if the first list ends, we reset the pointer to the head of another list
57 pA = (pA == NULL)?headB:pA->next;
58 pB = (pB == NULL)?headA:pB->next;
59 }
60 /**
61 given
62 A = [4,8,4,5],
63 B = [5,0,8,4,5]
64 in the 1st iteration pA go through 1 node less than pB,
65 in the 2nd iteration pA go through 1 node more than pB,
66 so they will meet at the intersection point
67 **/
68
69 return pA;
70 }
71};
Cost