{"id":27272,"date":"2018-01-20T20:27:19","date_gmt":"2018-01-20T14:57:19","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27272"},"modified":"2018-10-29T15:09:09","modified_gmt":"2018-10-29T09:39:09","slug":"java-programming-union-intersection-two-linked-lists","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-union-intersection-two-linked-lists\/","title":{"rendered":"Union and Intersection of two Linked Lists"},"content":{"rendered":"<p>Given two Linked Lists, create <a href=\"https:\/\/www.wikitechy.com\/technology\/union-find-algorithm-set-1-detect-cycle-undirected-graph\/\" target=\"_blank\" rel=\"noopener\">union<\/a> and <a href=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\" target=\"_blank\" rel=\"noopener\">intersection<\/a> lists that contain union and intersection of the elements present in the given lists. Order of elements in output lists doesn\u2019t matter.<span id=\"more-18615\"><\/span><\/p>\n<h3 id=\"example\"><span style=\"color: #993300;\">Example:<\/span><\/h3>\n<pre>Input:\r\n   List1: 10->15->4->20\r\n   List2:  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<figure id=\"attachment_31646\" aria-describedby=\"caption-attachment-31646\" style=\"width: 357px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-31646\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union.png\" alt=\"Union of two linked list\" width=\"357\" height=\"287\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union.png 1620w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-300x241.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-768x616.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-1024x822.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-990x794.png 990w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-1320x1059.png 1320w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/Union-260x210.png 260w\" sizes=\"(max-width: 357px) 100vw, 357px\" \/><figcaption id=\"caption-attachment-31646\" class=\"wp-caption-text\">Union<\/figcaption><\/figure>\n<figure id=\"attachment_31645\" aria-describedby=\"caption-attachment-31645\" style=\"width: 368px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-31645\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/intersection.png\" alt=\"intersection of two linked list\" width=\"368\" height=\"368\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/intersection.png 700w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/intersection-150x150.png 150w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/intersection-300x300.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/01\/intersection-65x65.png 65w\" sizes=\"(max-width: 368px) 100vw, 368px\" \/><figcaption id=\"caption-attachment-31645\" class=\"wp-caption-text\">Intersection<\/figcaption><\/figure>\n<h3 id=\"method-1-simple\"><span style=\"color: #003300;\"><strong>Method 1 (Simple):<\/strong><\/span><\/h3>\n<p>Following are simple algorithms to get union and intersection lists respectively.<\/p>\n<p><strong><em>Intersection (list1, list2):<\/em><\/strong><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><strong>Unio<em>n (list1, list2):<\/em><\/strong><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[ad type=\u201dbanner\u201d]\n<p>This method assumes that there are no duplicates in the given lists.<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%20%0A%20%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20void%20getUnion(Node%20head1%2C%20Node%20head2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20t1%20%3D%20head1%2C%20t2%20%3D%20head2%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t1%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20push(t1.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t1%20%3D%20t1.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t2%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!isPresent(head%2C%20t2.data))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20push(t2.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t2%20%3D%20t2.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20getIntersection(Node%20head1%2C%20Node%20head2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20result%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20Node%20t1%20%3D%20head1%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t1%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(isPresent(head2%2C%20t1.data))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20push(t1.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t1%20%3D%20t1.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while(temp%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(temp.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%0A%20%20%20%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%0A%20%20%20%20boolean%20isPresent%20(Node%20head%2C%20int%20data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20t%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(t%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(t.data%20%3D%3D%20data)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t%20%3D%20t.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist1%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20llist2%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20unin%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20intersecn%20%3D%20new%20LinkedList()%3B%0A%20%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20llist1.push(20)%3B%0A%20%20%20%20%20%20%20%20llist1.push(4)%3B%0A%20%20%20%20%20%20%20%20llist1.push(15)%3B%0A%20%20%20%20%20%20%20%20llist1.push(10)%3B%0A%20%0A%0A%20%20%20%20%20%20%20%20llist2.push(10)%3B%0A%20%20%20%20%20%20%20%20llist2.push(2)%3B%0A%20%20%20%20%20%20%20%20llist2.push(4)%3B%0A%20%20%20%20%20%20%20%20llist2.push(8)%3B%0A%20%0A%20%20%20%20%20%20%20%20intersecn.getIntersection(llist1.head%2C%20llist2.head)%3B%0A%20%20%20%20%20%20%20%20unin.getUnion(llist1.head%2C%20llist2.head)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22First%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20llist1.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Second%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20llist2.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Intersection%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20intersecn.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Union%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20unin.printList()%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\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><strong>Time Complexity: O(mn)<\/strong> 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<h3 id=\"method-2-use-merge-sort\"><span style=\"color: #003300;\"><strong>Method 2 (Use Merge Sort):<\/strong><\/span><\/h3>\n<p>In 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 \/>\n<strong>Following are the steps to be followed to get union and intersection lists:<\/strong><\/p>\n<p>1) Sort the first <a href=\"https:\/\/www.wikitechy.com\/technology\/quicksort-doubly-linked-list-3\/\" target=\"_blank\" rel=\"noopener\">Linked List<\/a> using merge sort. This step takes <strong>O(mLogm)<\/strong> time.<br \/>\n2) Sort the second Linked List using <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/merge-sort-algorithm-in-java\" target=\"_blank\" rel=\"noopener\">merge sort<\/a>. This step takes <strong>O(nLogn)<\/strong> time.<br \/>\n3) Linearly scan both sorted lists to get the union and intersection. This step takes <strong>O(m + n)<\/strong> time.<\/p>\n<p><strong>Time complexity<\/strong> of this method is<strong> O(mLogm + nLogn)<\/strong> which is better than method 1\u2019s time complexity.<\/p>\n[ad type=\u201dbanner\u201d]\n<h3 id=\"method-3-use-hashing\"><span style=\"color: #003300;\"><strong>Method 3 (Use Hashing):<\/strong><\/span><\/h3>\n<p><strong><em>Union (list1, list2)<\/em><\/strong><br \/>\nInitialize the result list as NULL and create an empty <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-hash-table\" target=\"_blank\" rel=\"noopener\">hash table<\/a>. 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><strong><em>Intersection (list1, list2)<\/em><\/strong><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><strong>Time complexity<\/strong> 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":[1,80128,2139],"tags":[81685,81966,83856,81687,81686,81690,81689],"class_list":["post-27272","post","type-post","status-publish","format-standard","hentry","category-coding","category-hashing","category-java","tag-intersection-of-two-linked-lists-c","tag-intersection-of-two-linked-lists-java","tag-intersection-of-two-linked-lists-leetcode-c","tag-intersection-of-two-linked-lists-python","tag-intersection-of-two-sorted-linked-lists-java","tag-union-and-intersection-of-two-linked-lists-in-java","tag-union-and-intersection-of-two-linked-lists-using-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27272","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=27272"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27272\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}