{"id":27270,"date":"2018-01-05T22:21:00","date_gmt":"2018-01-05T16:51:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27270"},"modified":"2018-01-05T22:21:00","modified_gmt":"2018-01-05T16:51:00","slug":"c-programming-union-intersection-two-linked-lists-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-union-intersection-two-linked-lists-2\/","title":{"rendered":"C Programming &#8211; Union and Intersection of two Linked Lists"},"content":{"rendered":"<p>Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. Order of elments in output lists doesn\u2019t matter.<span id=\"more-18615\"><\/span><\/p>\n<p>Example:<\/p>\n<pre>Input:\r\n   List1: 10->15->4->20\r\n   lsit2:  8->4->2->10\r\nOutput:\r\n   Intersection List: 4->10\r\n   Union List: 2->8->20->4->15->10\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Method 1 (Simple)<\/strong><br \/>\nFollowing are simple algorithms to get union and intersection lists respectively.<\/p>\n<p><em>Intersection (list1, list2)<\/em><br \/>\nInitialize result list as NULL. Traverse list1 and look for its each element in list2, if the element is present in list2, then add the element to result.<\/p>\n<p>Unio<em>n (list1, list2):<\/em><br \/>\nInitialize result list as NULL. Traverse list1 and add all of its elements to the result.<br \/>\nTraverse list2. If an element of list2 is already present in result then do not insert it to result, otherwise insert.<\/p>\n<p>This method assumes that there are no duplicates in the given lists.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%2FC%2B%2B%20program%20to%20find%20union%20and%20intersection%20of%20two%20unsorted%20%0A%2F%2F%20linked%20lists%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20A%20utility%20function%20to%20insert%20a%20node%20at%20the%20beginning%20of%20%0A%20%20%20a%20linked%20list*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%3B%0A%20%0A%2F*%20A%20utility%20function%20to%20check%20if%20given%20data%20is%20present%20in%20a%20list%20*%2F%0Abool%20isPresent(struct%20node%20*head%2C%20int%20data)%3B%0A%20%0A%2F*%20Function%20to%20get%20union%20of%20two%20linked%20lists%20head1%20and%20head2%20*%2F%0Astruct%20node%20*getUnion(struct%20node%20*head1%2C%20struct%20node%20*head2)%0A%7B%0A%20%20%20%20struct%20node%20*result%20%3D%20NULL%3B%0A%20%20%20%20struct%20node%20*t1%20%3D%20head1%2C%20*t2%20%3D%20head2%3B%0A%20%0A%20%20%20%20%2F%2F%20Insert%20all%20elements%20of%20list1%20to%20the%20result%20list%0A%20%20%20%20while%20(t1%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20push(%26result%2C%20t1-%3Edata)%3B%0A%20%20%20%20%20%20%20%20t1%20%3D%20t1-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Insert%20those%20elements%20of%20list2%20which%20are%20not%0A%20%20%20%20%2F%2F%20present%20in%20result%20list%0A%20%20%20%20while%20(t2%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(!isPresent(result%2C%20t2-%3Edata))%0A%20%20%20%20%20%20%20%20%20%20%20%20push(%26result%2C%20t2-%3Edata)%3B%0A%20%20%20%20%20%20%20%20t2%20%3D%20t2-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20result%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20get%20intersection%20of%20two%20linked%20lists%0A%20%20head1%20and%20head2%20*%2F%0Astruct%20node%20*getIntersection(struct%20node%20*head1%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20struct%20node%20*head2)%0A%7B%0A%20%20%20%20struct%20node%20*result%20%3D%20NULL%3B%0A%20%20%20%20struct%20node%20*t1%20%3D%20head1%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20list1%20and%20search%20each%20element%20of%20it%20in%0A%20%20%20%20%2F%2F%20list2.%20If%20the%20element%20is%20present%20in%20list%202%2C%20then%0A%20%20%20%20%2F%2F%20insert%20the%20element%20to%20result%0A%20%20%20%20while%20(t1%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isPresent(head2%2C%20t1-%3Edata))%0A%20%20%20%20%20%20%20%20%20%20%20%20push%20(%26result%2C%20t1-%3Edata)%3B%0A%20%20%20%20%20%20%20%20t1%20%3D%20t1-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20result%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20insert%20a%20node%20at%20the%20begining%20of%20a%20linked%20list*%2F%0Avoid%20push%20(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20*%2F%0A%20%20%20%20new_node-%3Edata%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20print%20a%20linked%20list*%2F%0Avoid%20printList%20(struct%20node%20*node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf%20(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20that%20returns%20true%20if%20data%20is%20%0A%20%20%20present%20in%20linked%20list%20else%20return%20false%20*%2F%0Abool%20isPresent%20(struct%20node%20*head%2C%20int%20data)%0A%7B%0A%20%20%20%20struct%20node%20*t%20%3D%20head%3B%0A%20%20%20%20while%20(t%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(t-%3Edata%20%3D%3D%20data)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20t%20%3D%20t-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%200%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head1%20%3D%20NULL%3B%0A%20%20%20%20struct%20node*%20head2%20%3D%20NULL%3B%0A%20%20%20%20struct%20node*%20intersecn%20%3D%20NULL%3B%0A%20%20%20%20struct%20node*%20unin%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*create%20a%20linked%20lits%2010-%3E15-%3E5-%3E20%20*%2F%0A%20%20%20%20push%20(%26head1%2C%2020)%3B%0A%20%20%20%20push%20(%26head1%2C%204)%3B%0A%20%20%20%20push%20(%26head1%2C%2015)%3B%0A%20%20%20%20push%20(%26head1%2C%2010)%3B%0A%20%0A%20%20%20%20%2F*create%20a%20linked%20lits%208-%3E4-%3E2-%3E10%20*%2F%0A%20%20%20%20push%20(%26head2%2C%2010)%3B%0A%20%20%20%20push%20(%26head2%2C%202)%3B%0A%20%20%20%20push%20(%26head2%2C%204)%3B%0A%20%20%20%20push%20(%26head2%2C%208)%3B%0A%20%0A%20%20%20%20intersecn%20%3D%20getIntersection%20(head1%2C%20head2)%3B%0A%20%20%20%20unin%20%3D%20getUnion%20(head1%2C%20head2)%3B%0A%20%0A%20%20%20%20printf%20(%22%5Cn%20First%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList%20(head1)%3B%0A%20%0A%20%20%20%20printf%20(%22%5Cn%20Second%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList%20(head2)%3B%0A%20%0A%20%20%20%20printf%20(%22%5Cn%20Intersection%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList%20(intersecn)%3B%0A%20%0A%20%20%20%20printf%20(%22%5Cn%20Union%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList%20(unin)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre> First list is\r\n10 15 4 20\r\n Second list is\r\n8 4 2 10\r\n Intersection list is\r\n4 10\r\n Union list is\r\n2 8 20 4 15 10\r\n<\/pre>\n<p>Time Complexity: O(mn) for both union and intersection operations. Here m is the number of elements in first list and n is the number of elements in second list.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Method 2 (Use Merge Sort)<\/strong><br \/>\nIn this method, algorithms for Union and Intersection are very similar. First we sort the given lists, then we traverse the sorted lists to get union and intersection.<br \/>\nFollowing are the steps to be followed to get union and intersection lists.<\/p>\n<p>1) Sort the first Linked List using merge sort. This step takes O(mLogm) time. Refer <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/7740\">this post<\/a> for details of this step.<br \/>\n2) Sort the second Linked List using merge sort. This step takes O(nLogn) time. Refer <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/7740\">this post<\/a> for details of this step.<br \/>\n3) Linearly scan both sorted lists to get the union and intersection. This step takes O(m + n) time. This step can be implemented using the same algorithm as sorted arrays algorithm discussed <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/6873\">here<\/a>.<\/p>\n<p>Time complexity of this method is O(mLogm + nLogn) which is better than method 1\u2019s time complexity.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Method 3 (Use Hashing)<\/strong><br \/>\n<em>Union (list1, list2)<\/em><br \/>\nInitialize the result list as NULL and create an empty hash table. Traverse both lists one by one, for each element being visited, look the element in hash table. If the element is not present, then insert the element to result list. If the element is present, then ignore it.<\/p>\n<p><em>Intersection (list1, list2)<\/em><br \/>\nInitialize the result list as NULL and create an empty hash table. Traverse list1. For each element being visited in list1, insert the element in hash table. Traverse list2, for each element being visited in list2, look the element in hash table. If the element is present, then insert the element to result list. If the element is not present, then ignore it.<\/p>\n<p>Both of the above methods assume that there are no duplicates.<\/p>\n<p>Time complexity of this method depends on the hashing technique used and the distribution of elements in input lists. In practical, this approach may turn out to be better than above 2 methods.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,80128],"tags":[73367,70049,79591,79586,73373,72479,81317,72493,73337,81311,79623,2124,73381,79582,73372,75614,79507],"class_list":["post-27270","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-hashing","tag-application-of-linked-list","tag-linked-list","tag-linked-list-in-c-code","tag-linked-list-in-c-tutorial","tag-linked-list-in-cpp","tag-linked-list-in-data-structure","tag-linked-list-interview-questions","tag-linked-list-program","tag-linked-list-program-in-c","tag-linked-list-program-in-cpp","tag-linked-list-tutorial","tag-list-of-data-types","tag-singly-linked-list-in-c-program","tag-singly-linked-list-in-java","tag-singly-linked-list-program-in-c","tag-stack-using-linked-list","tag-what-is-linked-list-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27270"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27270\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}