Given two linked lists, calculate the Union (linked list) and intersection (linked list) of the two linked lists ). Elements in the Union (linked list) and intersection (linked list) are not required to be ordered.
For example, enter:
List1: 10-> 15-> 4-> 20
List2: 8-> 4-> 2-> 10
Output:
Intersection (linked list): 4-> 10
Union (linked list): 2-> 8-> 20-> 4-> 15-> 10
Method 1 (simple and intuitive ):
The following is a simple algorithm for getting the Union and intersection of two linked lists.
Intersection (list1, list2): the initialization result is that the linked list is empty. For example, go to table 1 of The traversal chain and find each element in linked list 2. If this element exists in linked list 2, insert the element into the result linked list.
Union (list1, list2): the initialization result linked list is empty. All elements in linked list 1 are inserted into the result linked list. This element is inserted in the result linked list. Otherwise, the element is skipped.
#include <stdio.h>#include <stdlib.h>/*Link list node*/struct node{int data;struct node* next;};/* A utility function to insert a node at the begining of a linked list */void push(struct node **head_ref, int new_data);/* A utility function to chec if given data is present in a list */bool isPresent(struct node *head, int data);/* Function to get union of two linked lists head1 and head2*/struct node *getUnion(struct node *head1, struct node *head2){struct node *result = NULL;struct node *t1 = head1, *t2 = head2;//Insert all elements of list1 to result listwhile(t1 != NULL){push(&result, t1->data);t1 = t1->next;}//Insert those elements of list2 which are not present in result listwhile(t2 != NULL){if(!isPresent(result, t2->data))push(&result, t2->data);t2 = t2->next;}return result;}/* Function to get intersection of two linked lists head1 and head2 */struct node *getIntersection(struct node *head1, struct node *head2){struct node *result = NULL;struct node *t1 = head1;//Traverse list1 and search each element of it in list2. If the element//is present in list2, then insert the element to resultwhile( t1 != NULL ){if(isPresent(head2, t1->data))push(&result, t1->data);t1 = t1->next;}return result;}/* A utility function to insert a node at the begining of a linked list */void push(struct node**head_ref, int new_data){/*allocate node*/struct node* new_node = (struct node*)malloc(sizeof(struct node));/* put in the data */new_node->data = new_data;/*link the old list off the new node*/new_node->next = (*head_ref);/* move the head to point to the new node*/(*head_ref) = new_node;}/*A utility function fto print a linked list*/void printList(struct node *node){while( node != NULL ){printf("%d ", node->data);node = node->next;}}/*A utility function that returns true if data is present in linked list else reurn false */bool isPresent(struct node *head, int data){struct node *t = head;while(t != NULL){if( t->data == data )return 1;t = t->next;}return 0;}/* Drier program to test above function*/int main(){/* Start with the empty list */struct node* head1 = NULL;struct node* head2 = NULL;struct node* intersecn = NULL;struct node* unin = NULL;/*create a linked lits 10->15->5->20 */push (&head1, 20);push (&head1, 4);push (&head1, 15);push (&head1, 10);/*create a linked lits 8->4->2->10 */push (&head2, 10);push (&head2, 2);push (&head2, 4);push (&head2, 8);intersecn = getIntersection (head1, head2);unin = getUnion (head1, head2);printf ("\n First list is \n");printList (head1);printf ("\n Second list is \n");printList (head2);printf ("\n Intersection list is \n");printList (intersecn);printf ("\n Union list is \n");printList (unin);printf("\n"); return 0;}
Time Complexity: In this program, the time complexity of the Union and intersection operations of the linked list is O (Mn), M is the number of elements in the Linked List 1, and N is the element in the Linked List 2.
Method 2 (sort by merge ):
Using this method, the Union and intersection operations of two linked lists are very similar. First, sort the two linked lists and traverse the two linked lists to obtain the intersection and union of the two tables.
The specific implementation steps are as follows:
Sort 1st linked lists by merging. the time complexity of this operation is O (mlogm). [click here for details]
Sort the 2nd linked lists in the Merge Sorting heap. the time complexity of this operation is O (nlogn ).
Traverse two ordered linked lists linearly to obtain the intersection and union of the two linked lists. The time complexity of this operation is O (m + n). [This step is similar to finding the intersection and Union of Ordered arrays. The latter has been implemented before. Click here for details]
The time complexity of this method is O (mlogm + nlogn), which is better than the first method.
Method 3 (hash ):
Union (list1, list2)
First, the initialization result linked list is null. Create an empty hash table, traverse the two linked lists, and insert the elements in the linked list into the hash table, when an element is inserted, check whether the element exists in the hash table. If the element does not exist in the hash table, insert the element to the result linked list. If the hash table already exists, ignore this element and continue traversing the next element.
Intersection (list1, list2)
First, the initialization result linked list is null. Create an empty hash table, traverse list1, and insert every element in list1 into the hash table. Then traverse list2. If the element in list2 already exists in the hash table, insert the element to the result linked list. If the element does not exist in the hash table, ignore the element, continue to traverse the next element.
The efficiency of this method depends on the Implementation Technology of the hash table. Generally, this method is better than the above two methods.
Original address: http://www.geeksforgeeks.org/archives/18615? Utm_source = RSS & utm_medium = RSS & utm_campaign = Union-and-intersection-of-two-linked-lists