Finding the set and intersection of linked lists

Source: Internet
Author: User

Given 2 linked lists, the set (linked list) and intersection (linked list) of the 2 linked lists are obtained. The elements in the set (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

Set (linked list): 2->8->20->4->15->10

Method One (simple, intuitive method):

Here is a simple algorithm that gets the set and intersection of 2 lists.

Intersection (LIST1,LIST2): Initialize the result of the list is empty, traverse the list 1, in the list 2 to find each of its elements, if the linked List 2 also has this element, then insert the element into the result list.

Union (LIST1,LIST2): The initialization result list is empty, and all elements in the list 1 are inserted into the result list. Iterate through the chain List 2, if the result list does not have the element, then insert, otherwise skip the element.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*link List node*/
  4. struct node
  5. {
  6. int data;
  7. struct node* next;
  8. };
  9. /* A utility function to insert a node at the begining of a linked list */
  10. void push (struct node **head_ref, int new_data);
  11. /* A utility function to CHEC if given data are present in a list */
  12. BOOL IsPresent (struct node *head, int data);
  13. /* Function to get union of linked lists Head1 and head2*/
  14. struct node *getunion (struct node *head1, struct node *head2)
  15. {
  16. struct node *result = NULL;
  17. struct Node *t1 = head1, *t2 = head2;
  18. //insert all elements of list1 to result list
  19. While (t1! = NULL)
  20. {
  21. Push (&result, t1->data);
  22. T1 = t1->next;
  23. }
  24. //insert Those elements of list2 which is not present in result list
  25. While (t2! = NULL)
  26. {
  27. if (!ispresent (result, t2->data))
  28. Push (&result, t2->data);
  29. T2 = t2->next;
  30. }
  31. return result;
  32. }
  33. /* Function to get intersection of linked lists Head1 and head2 */
  34. struct node *getintersection (struct node *head1, struct node *head2)
  35. {
  36. struct node *result = NULL;
  37. struct node *t1 = head1;
  38. //traverse List1 and search each element of the it in List2. If the element
  39. //is present in List2, then insert the element to result
  40. While (t1! = NULL)
  41. {
  42. if (isPresent (head2, t1->data))
  43. Push (&result, t1->data);
  44. T1 = t1->next;
  45. }
  46. return result;
  47. }
  48. /* A utility function to insert a node at the begining of a linked list */
  49. void push (struct node**head_ref, int new_data)
  50. {
  51. /*allocate node*/
  52. struct node* new_node = (struct node*) malloc (sizeof (struct node));
  53. / * Put in the data * /
  54. New_node->data = New_data;
  55. /*link The old list off the new node*/
  56. New_node->next = (*head_ref);
  57. / * Move the head to point to the new node*/
  58. (*head_ref) = New_node;
  59. }
  60. /*a utility function FTO print A linked list*/
  61. void Printlist (struct node *node)
  62. {
  63. While (node! = NULL)
  64. {
  65. printf ("%d", node->data);
  66. node = node->next;
  67. }
  68. }
  69. /*a utility function that returns TRUE if the data is present in
  70. Linked list Else Reurn false */
  71. BOOL IsPresent (struct node *head, int data)
  72. {
  73. struct Node *t = head;
  74. While (t! = NULL)
  75. {
  76. if (t->data = = data)
  77. return 1;
  78. t = t->next;
  79. }
  80. return 0;
  81. }
  82. /* Drier program to test above function*/
  83. int main ()
  84. {
  85. / * Start with the empty list * /
  86. struct node* head1 = NULL;
  87. struct node* head2 = NULL;
  88. struct node* INTERSECN = NULL;
  89. struct node* unin = NULL;
  90. /*create a linked lits 10->15->5->20 * /
  91. Push (&head1, 20);
  92. Push (&HEAD1, 4);
  93. Push (&head1, 15);
  94. Push (&head1, 10);
  95. /*create a linked lits 8->4->2->10 * /
  96. Push (&head2, 10);
  97. Push (&HEAD2, 2);
  98. Push (&HEAD2, 4);
  99. Push (&HEAD2, 8);
  100. INTERSECN = Getintersection (Head1, head2);
  101. Unin = Getunion (Head1, head2);
  102. printf ("\ n first list is \ n");
  103. Printlist (HEAD1);
  104. printf ("\ n Second list is \ n");
  105. Printlist (HEAD2);
  106. printf ("\ n intersection list is \ n");
  107. Printlist (INTERSECN);
  108. printf ("\ n Union list is \ n");
  109. Printlist (Unin);
  110. printf ("\ n");
  111. return 0;
  112. }

Time complexity: In this program, the time complexity of the linked list and the intersection operation is O (MN), M is the number of elements in the list 1, n is the element of the list 2.

Method 2 (Use merge sort):

Using this method, it is very similar to the operation of the set and intersection of the 2 linked lists. First, the 2 linked lists are sorted, and then the 2 linked lists are traversed, resulting in 2 table intersections and a set.

Here are the specific implementation steps:

    1. The 1th list is sorted by a merge sort, and the time complexity of this operation is O (MLOGM). [Click here to see more]

    2. Sorted by the 2nd linked list of the merge sort heap, the time complexity of this operation is O (NLOGN).

    3. Linear traversal of 2 ordered linked lists, the intersection and the set of 2 linked lists are obtained. The time complexity of this operation is O (m+n). [This step is similar to finding the intersection and the set of an ordered array, which has been implemented before, click here to see more]

The time complexity of this method is O (mlogm+ Nlogn), which is better than the first method.

Method 3 (hash method):

Union (List1, List2)

First initialize the result linked list is null, create an empty hash table, traverse two linked lists, insert the elements of the list into the hash table, insert the element while checking the hash table when the element is already present, if the hash table does not exist in the element, the element is also inserted into the result list, If the hash table already exists, the element is ignored and the next element continues to traverse.

Intersection (List1, List2)

First initialize the result linked list to NULL, create an empty hash table, traverse List1, and insert each element in the List1 into the hash table. Then traverse List2, for elements in the List2, if it already exists in the hash table, then insert the element into the result list, if it does not exist with the hash table, then ignore the element, continue to traverse the next element.

The efficiency of this method depends on the implementation of the hash table technology, in general, this method is better than the above two.

Original address: http://www.geeksforgeeks.org/archives/18615?utm_source=rss&utm_medium=rss&utm_campaign= Union-and-intersection-of-two-linked-lists

Finding the set and intersection of linked lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.