-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListIntersection.cpp
More file actions
163 lines (129 loc) · 3.82 KB
/
Copy pathLinkedListIntersection.cpp
File metadata and controls
163 lines (129 loc) · 3.82 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Given 2 sorted linked lists, find the intersection.
// For example - Input: List 1 - 1, 3, 5, 10, 54, 78
// List 2 - 6, 10, 54, 98, 99
// Output 10, 54
#include <iostream>
#include "Node.h"
using namespace std;
// Insert a new node to the list
void insert(Node** root, int item);
// Create a linked list with given array list
Node* arrayToList(int arr[], size_t arrSize);
// Function to get the size of the linked list (# of nodes)
int getLinkedListCount(Node* head);
// Function to get the intersection when one list has more nodes than the other list
void getIntersection(Node* head1, Node* head2);
// Function to get the intersection of equal length lists
void getIntersectionHelper(Node* head1, Node* head2);
int main()
{
// Initialize variables
int listArray[] {1, 3, 5, 10, 10, 54, 78, 82};
int listArray2[] = {1, 3, 10, 10, 25, 78};
// Get array sizes
size_t arrSize1 = sizeof(listArray)/sizeof(listArray[0]);
size_t arrSize2 = sizeof(listArray)/sizeof(listArray[0]);
Node* list1 = arrayToList(listArray, arrSize1);
Node* list2 = arrayToList(listArray2, arrSize2);
// Get the intersection of the two lists
getIntersection(list1, list2);
return 0;
}
// Function Implementations
void insert(Node** head, int item)
{
Node* temp = new Node;
Node* ptr;
// Set the item to the new node temp data
// set the next item to null
temp->data = item;
temp->next = NULL;
if(*head == NULL)
{
*head = temp;
}
else
{
ptr = *head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
Node* arrayToList(int arr[], size_t arrSize)
{
int index;
Node* head = NULL;
for(index = 0; index < arrSize; index++)
{
insert(&head, arr[index]);
}
return head;
}
int getLinkedListCount(Node* head)
{
// Initialize variables
int count = 0;
// Set the current pointer to the head of the list
Node* current = head;
// start the count until it hits NULL. Break after and return count
while(current != NULL)
{
// increment counter
count++;
// move the pointers
current = current->next;
}
return count;
}
void getIntersection(Node* head1, Node* head2)
{
// Initialize variables
// get the sizes of both linked lists
int linkedListSize1 = getLinkedListCount(head1);
int linkedListSize2 = getLinkedListCount(head2);
// check if the first list has more nodes than the second list
if(linkedListSize1 > linkedListSize2)
{
getIntersectionHelper(head1, head2);
}
// otherwise, the opposite
else
{
getIntersectionHelper(head2, head1);
}
}
// This functions starts out with the bigger list to compare and increment
void getIntersectionHelper(Node* head1, Node* head2)
{
// Initialize variables
int index;
Node* current1 = head1;
Node* current2 = head2;
// Move the pointers of both lists till a match is found
while(current1 != NULL && current2 != NULL)
{
// Check if either list is longer than the other
if(current1->data == current2->data)
{
cout << current2->data << " ";
// move both pointers
current1 = current1->next;
current2 = current2->next;
}
// Compare the data itself to each other to see if they match
// Current data in list 1 is less than the current data in list 2
else if(current1->data < current2->data)
{
// return the data
current1 = current1->next;
}
// Current data in list 2 is less than the current data in list 1
else if(current2->data < current1->data)
{
current2 = current2->next;
}
}
}