-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlist-common-node.cpp
68 lines (60 loc) · 1.54 KB
/
list-common-node.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
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
// http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
void getIntersection(Node *head1, Node *head2);
void printList(Node *head);
void getIntersection(Node *head1, Node *head2) {
if (head1 == NULL || head2 == NULL)
return;
bool visited[100] = {false};
// mark nodes in list 1 as visited
while (head1 != NULL) {
visited[head1->data] = true;
head1 = head1->next;
}
// traverse list 2 and return if any node is already visited
while (head2 != NULL) {
if (visited[head2->data]) {
cout<<head2->data;
return;
}
head2 = head2->next;
}
cout<<"none";
}
void printList(Node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main()
{
Node *head1 = new Node(3);
head1->next = new Node(6);
head1->next->next = new Node(9);
head1->next->next->next = new Node(15);
head1->next->next->next->next = new Node(30);
Node *head2 = new Node(10);
head2->next = head1->next->next->next;
head2->next->next = head1->next->next->next->next;
cout<<"list 1: ";
printList(head1);
cout<<endl<<"list 2: ";
printList(head2);
cout<<endl<<"common node: ";
getIntersection(head1, head2);
cout<<endl;
return 0;
}