-
Notifications
You must be signed in to change notification settings - Fork 7
/
LinkedListIntersection.cpp
38 lines (33 loc) · 993 Bytes
/
LinkedListIntersection.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
// Problem: https://leetcode.com/problems/intersection-of-two-linked-lists/
#include <stack>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class LinkedListIntersection {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
stack<ListNode*> s1 = toStack(headA);
stack<ListNode*> s2 = toStack(headB);
ListNode* intersection = nullptr;
while (not s1.empty() && not s2.empty()) {
ListNode* n1 = s1.top(); s1.pop();
ListNode* n2 = s2.top(); s2.pop();
if (n1 == n2) intersection = n1;
else break;
}
return intersection;
}
stack<ListNode*> toStack(ListNode* head) {
stack<ListNode*> st;
ListNode* curr = head;
while (curr != nullptr) {
st.push(curr);
curr = curr->next;
}
return st;
}
};