forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
160.cpp
36 lines (33 loc) · 841 Bytes
/
160.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
/**
* 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) {
ListNode *pA = headA,*pB = headB;
if(!pA || !pB) return NULL;
while(pA || pB){
if(pA == pB) return pA;
else{
if(!pA) {
pA = headB;
pB = pB -> next;
}
else if(!pB) {
pB = headA;
pA = pA -> next;
}
else {
pA = pA -> next;
pB = pB -> next;
}
}
}
return NULL;
}
};