-
Notifications
You must be signed in to change notification settings - Fork 0
/
160.intersection-of-two-linked-lists.cpp
52 lines (51 loc) · 1.35 KB
/
160.intersection-of-two-linked-lists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// version 1: figure out the length of A and B, the longer one move difference length first
/*
int la = 0, lb = 0;
ListNode *cA = headA, *cB = headB;
while (cA) {
cA = cA->next;
la++;
}
while (cB) {
cB = cB->next;
lb++;
}
cout<<la<<lb;
if (la < lb) {
swap(headA, headB);
}
for (int i = 0; i < abs(la - lb); i++) {
headA = headA->next;
}
while (headA && headB) {
if (headA == headB) {
return headA;
}
headA = headA->next;
headB = headB->next;
}
return NULL;
*/
// version 2: one node traverse from A start to A end then to B start, another node also do it, the node they meet is the result.
if (!headA || !headB) {
return NULL;
}
ListNode *tA = headA, *tB = headB;
while (tA != tB) {
tA = tA ? tA->next: headB;
tB = tB ? tB->next: headA;
}
return tA;
}
};