Skip to content

Commit 02b36a8

Browse files
authored
Merge pull request neetcode-gh#2400 from Ritesh7766/main
Create 0160-intersection-of-two-linked-lists.cpp
2 parents 69fe8c8 + a1ca737 commit 02b36a8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
10+
class Solution {
11+
public:
12+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
13+
ListNode *trevA = headA, *trevB = headB;
14+
15+
while (trevA != trevB) {
16+
trevA = (trevA != NULL) ? trevA->next : headA;
17+
trevB = (trevB != NULL) ? trevB->next : headB;
18+
}
19+
return trevA;
20+
}
21+
};

0 commit comments

Comments
 (0)