-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day102.cpp
40 lines (35 loc) · 949 Bytes
/
Day102.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
37
38
39
40
// Intersection of Linked Lists
// https://www.interviewbit.com/problems/intersection-of-linked-lists/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::getIntersectionNode(ListNode* A, ListNode* B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
ListNode* l1 = A;
ListNode* l2 = B;
while(l1 != l2) {
if(l1 == NULL) {
l1 = B;
}
else {
l1 = l1 -> next;
}
if(l2 == NULL) {
l2 = A;
}
else {
l2 = l2 -> next;
}
}
if(l1 != NULL) {
return l1;
}
}