diff --git a/linkedlist/intersection_linkedlist.cpp b/linkedlist/intersection_linkedlist.cpp new file mode 100644 index 0000000..e5e0f84 --- /dev/null +++ b/linkedlist/intersection_linkedlist.cpp @@ -0,0 +1,86 @@ +/* +Author: Divya Gupta +C++ implementation to find intersection of 2 linked list +*/ +#include +using namespace std; +/* Link list node */ +struct Node +{ + int value; + struct Node* next; +}; + +/* function to insert a node at the beginning of a linked list*/ +void insert(struct Node** head, int data) +{ + struct Node* node = (struct Node*) malloc(sizeof(struct Node)); + node->value=data; + node->next=*head; + *head=node; +} +/*function to check if given data is present in a list */ +bool check(struct Node *head, int data) +{ + struct Node* node=head; + while (node!= NULL) + { + if (node->value == data) + return 1; + node= node->next; + } + return 0; +} +/* Function to get intersection of two linked lists + head1 and head2 */ +struct Node *getIntersection(struct Node *head1,struct Node *head2) +{ + struct Node *intersection = NULL; + struct Node *t1 = head1; + while (t1 != NULL) + { + if (check(head2, t1->value)) + insert(&intersection, t1->value); + t1 = t1->next; + } + + return intersection; +} +/* A function to print a linked list*/ +void printList (struct Node *node) +{ + while (node != NULL) + { + cout<value<<" "; + node = node->next; + } +} +int main() +{ + /* Start with the empty list */ + struct Node* head1 = NULL; + struct Node* head2 = NULL; + struct Node* intersect= NULL; + int l1,l2,a; + cout<<"Enter size of first linked list ="<>l1; + cout<<"Enter size of second linked list = "<>l2; + cout<<"Enter elements of 1st linked list "<>a; + insert(&head1,a); + } + cout<<"Enter elements of 2nd linked list "<>a; + insert(&head2,a); + } + intersect = getIntersection (head1, head2); + cout<<"intersection of 2 linked list"<