 ronith
7/7/2018 - 6:01 PM

## Intersection of two Sorted Linked Lists

Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.

For example, let the first linked list be 1->2->3->4->6 and second linked list be 2->4->6->8, then your function should create and return a third list as 2->4->6.

``````// https://www.geeksforgeeks.org/intersection-of-two-sorted-linked-lists/
#include <bits/stdc++.h>
using namespace std;

int data;
};
typedef struct linked_list node;

void print (node* n) {
while (n != NULL) {
cout<< n->data<< "->";
n = n->next;
}
}

node* insert(node* head, int n) {
if (head == NULL) {
node* list= (node*)malloc(sizeof(node*));
list->data = n;
list->next = NULL;
}

node* list= (node*)malloc(sizeof(node*));
list->data = n;
list->next = NULL;
while (last->next != NULL )
last = last->next;
last->next = list;
}

node* intersection (node *head1, node *head2) {
node* head3 = NULL;
}
}
}

int main() {
int n;
cout<< "Enter the first list: \n";
while (true) {
cout<< "Enter the elements, and -999 to stop: ";
cin>>n;
if (n==-9)
break;
else
}
cout<< "Enter the second list: \n";
while (true) {
cout<< "Enter the elements, and -999 to stop: ";
cin>>n;
if (n==-9)
break;
else