-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0160.h
48 lines (45 loc) · 1.18 KB
/
0160.h
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
#include "common.h"
using namespace std;
namespace s0160 {
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = 0, lenB = 0;
ListNode* t1 = headA, *t2 = headB;
while (t1) {
lenA++;
t1 = t1->next;
}
while (t2) {
lenB++;
t2 = t2->next;
}
t1 = headA;
t2 = headB;
if (lenB > lenA) {
swap(lenA, lenB);
swap(t1, t2);
}
int val = lenA - lenB;
while(val--) {
t1 = t1->next;
}
while (t1) {
if (t1 == t2) {
return t1;
}
t1 = t1->next;
t2 = t2->next;
}
return nullptr;
}
};
}