Skip to content

Commit 5f948aa

Browse files
Time: 37 ms (68.92%), Space: 17.4 MB (36.58%) - LeetHub
1 parent b74463b commit 5f948aa

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
12+
if (headA == NULL || headB == NULL) {
13+
return NULL;
14+
}
15+
ListNode* temp1 = headA;
16+
ListNode* temp2 = headB;
17+
18+
while (temp1 != temp2) {
19+
temp1 = temp1->next;
20+
temp2 = temp2->next;
21+
if (temp1 == temp2) {
22+
return temp1;
23+
}
24+
if (temp1 == NULL) {
25+
temp1 = headB;
26+
}
27+
if (temp2 == NULL) {
28+
temp2 = headA;
29+
}
30+
}
31+
return temp1;
32+
}
33+
};

0 commit comments

Comments
 (0)