Skip to content

Commit 92e009a

Browse files
authored
Create 160_Intersection of Two Linked Lists.cpp
1 parent 14f7227 commit 92e009a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
13+
ListNode *p = headA;
14+
ListNode *q = headB;
15+
16+
while(p != q){
17+
18+
if(!p)
19+
p = headB;
20+
else
21+
p = p->next;
22+
if(!q)
23+
q = headA;
24+
else
25+
q = q->next;
26+
}
27+
return p;
28+
}
29+
};

0 commit comments

Comments
 (0)