-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintersection_linked_list.cpp
More file actions
122 lines (104 loc) · 3.46 KB
/
intersection_linked_list.cpp
File metadata and controls
122 lines (104 loc) · 3.46 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Created by Mayank Parasar on 2019-12-08.
//
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
// ctor-1
ListNode(int x) : val(x), next(nullptr)
{}
// ctor-2
ListNode(int x, ListNode* node) : val(x), next(node)
{}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *intersection = new ListNode(0);
ListNode *nodeA = headA;
ListNode *nodeB = headB;
// Loop through list-A
while(nodeA != nullptr) {
nodeB = headB; // for every itneration restart listB
while(nodeB != nullptr) {
if(nodeA->val == nodeB->val) {
// loop here both nodes to know they are same onwards
// else break
ListNode *checkNodeA = nodeA;
ListNode *checkNodeB = nodeB;
while (checkNodeA != nullptr && checkNodeB!= nullptr) {
if ( checkNodeA->val == checkNodeB->val ) {
checkNodeA = checkNodeA->next;
checkNodeB = checkNodeB->next;
}
else {
break;
}
}
if (checkNodeA == nullptr && checkNodeB == nullptr)
return nodeA;
}
nodeB = nodeB->next;
}
nodeA = nodeA->next;
}
// for each element in list A loop list-B
// if the element is found in the listB
// return that element
// at the end of this loop if control reaches here
// then there is no match... put an assert
assert(nodeA->next == nullptr &&
nodeB->next == nullptr);
return intersection;
}
void printList(ListNode *head) {
ListNode * node = head; // starting node
while (node != nullptr) {
cout << node->val << " -> ";
node = node->next;
}
cout << endl;
return;
}
};
int main() {
Solution sol;
// let the list-1 A = 1 -> 2 -> 3 -> 4
ListNode *headA = new ListNode(1);
(headA->next) = new ListNode(2);
(headA->next->next) = new ListNode(3);
(headA->next->next->next) = new ListNode(4);
sol.printList(headA);
// let the list-2 B = 6 -> 3 -> 4
ListNode *headB = new ListNode(6);
(headB->next) = new ListNode(3);
(headB->next->next) = new ListNode(4);
sol.printList(headB);
// [4,1,8,4,5]
// [5,0,1,8,4,5]
ListNode *headC = new ListNode(4);
(headC->next) = new ListNode(1);
(headC->next->next) = new ListNode(8);
(headC->next->next->next) = new ListNode(4);
(headC->next->next->next->next) = new ListNode(5);
ListNode *headD = new ListNode(5);
(headD->next) = new ListNode(0);
(headD->next->next) = new ListNode(1);
(headD->next->next->next) = new ListNode(8);
(headD->next->next->next->next) = new ListNode(4);
(headD->next->next->next->next->next) = new ListNode(5);
// ListNode* sol_ = sol.getIntersectionNode(headA, headB);
ListNode* sol_ = sol.getIntersectionNode(headC, headD);
cout << sol_->val << " " << sol_->next << endl;
return 0;
}