Two Sorted LinkedList Intersection in JavaIn this problem, two sorted linked list (in nondecreasing order) is given. The task is to find the intersection of those two linked lists, i.e., finding those elements that are present in both of the linked lists. Example 1: Input: Linked List 1: 12 > 13 > 35 > 40 > 56 > 89 > 90 > 92 Linked List 2: 9 > 14 > 15 > 30 > 40 > 85 > 90 > 92 Output: 40 > 90 > 92 Let's see various approaches to solve this problem. Using Dummy NodeThe approach is to use a dummy or temporary node at the beginning of the output list. The tail pointer is a pointer that is pointing to the last node of the output list so that the new nodes can be added or appended quickly. The temporary node gives the tail a space of memory to point to. Note that in the beginning, the tail pointer points to the output dummy node. Now the task is to iterate over the given linked list using a single loop. Note that the loop will terminate if any of the linked lists is traversed completely. In the loop, twopointer approach is used. One pointer ptr1 for the linked list 1 and ptr2 for the linked list 2. There are the following three cases: Case 1: Value pointed by ptr1 is greater than value pointed by ptr2. Case 2: Value pointer by ptr1 is less than the value pointer by ptr2. Case 3: Value pointed by ptr1 and ptr2 is equal. For case 1, increment ptr2 to point to the next node. For case 2, increment ptr1 to point to the next node. For case 3, print the value and increment, both the pointers to point to the next node. In case 3, we have to take care of the nodes that contain the same value in the linked list. For example, Linked list 1: 4 > 4 > 4 > 4 > 4 > 4 > 5 Linked list 2: 4 > 4 > 4> 5 Here, case 3 applies, as the first node of both the linked list is 4. So, we print the value 4, and increment ptr1 and ptr2 to point to the next node. However, even after incrementing ptr1 and ptr2 both points to the same value 4. Therefore, we again have to increment both the pointers; but this time we will not print the value 4 as 4 is already printed, and we will keep on incrementing the pointers such that both the pointers point to the value 5. Hence, the output is 4 and 5. Note that intersection here is working in the same way we learnt in the intersection in the chapter called sets. Observe the following program. FileName: LinkedListIntersection.java Output: The first linked list is: 12 13 35 40 56 89 90 92 The second linked list is: 9 14 15 30 40 85 90 92 The common elements of the first & the second linked list are: 40 90 92 The first linked list is: 4 4 4 4 4 5 The second linked list is: 4 4 4 5 The common elements of the first & the second linked list are: 4 5 Time Complexity: The time complexity of the above program is O(min(a, b)), where a and b are the numbers of elements or nodes present in the linked list 1 and 2, respectively. Using RecursionUsing recursion also, we can achieve the same thing. FileName: LinkedListIntersection1.java Output: The first linked list is: 12 13 35 40 56 89 90 92 The second linked list is: 9 14 15 30 40 85 90 92 The common elements of the first & the second linked list are: 40 90 92 Using HashingAs we know that HashSet in Java contains only unique elements; hence, we can use this property to find the common elements from the given two linked lists. Observe the following program. FileName: LinkedListIntersection2.java Output: The first linked list is: 12 13 35 40 56 89 90 92 The second linked list is: 9 14 15 30 40 85 90 92 The common elements of the first & the second linked list are: 40 90 92 Time Complexity: As we are traversing both linked lists separately, therefore, the time complexity of the above program is O(a + b), where a and b are the total number of nodes present in the given linked lists.
Next TopicSet Matrix Zeros in Java
