Difficulty: Medium**, Asked-in:** Amazon, Microsoft, Yahoo.
Key takeaway: An excellent problem to learn problem-solving using two-pointers in a linked list.
Let’s understand the problem
Given two sorted linked lists, write a program to return the intersections of the linked lists.
- We need to return the head of the new linked list.
- Both linked lists have no cycle.
- If there is no common element, return NULL.
- We shouldn’t do any changes in the structure of the linked lists.
Example 1
Input: list1 = 2 -> 9 -> 13 -> 20 -> 22 -> 27 -> NULL, list2 = 2 -> 4 -> 9 -> 20 -> 27 -> NULL.
Output: 2 -> 9 -> 20 -> 27 -> NULL
Explanation: 2, 9, 20, and 27 are the common nodes to both linked lists.
Example 2
Input: list1 = 13 -> 22 -> 27 -> NULL, list2 = 4 -> 23 -> NULL.
Output: NULL
Explanation: No nodes are common to both linked lists.
Discussed solution approaches
- Two pointers approach using head and tail pointers
- Two pointers approach using dummy node
- Recursive version of the iterative approach
Two pointers approach using head and tail pointers
Solution idea
Both linked lists are sorted in increasing order. How can we use this information to find the common nodes? One idea is: Common nodes will be present in the same order in both sorted linked lists. For example, if one linked list is 2->7->9->12 ->17 and other linked list is 1->7->12->15->17, then the order of common nodes will be the same i.e. (7, 12, 17).
So using the sorted order property, we can scan both lists from the start, compare values one by one and generate the intersection list. When both node values are the same, we add that node to the output. Otherwise, whichever node value is smaller, we move forward in that list.
At any intermediate point, suppose we are at a node x in list1 and node y in list2.
- If x.data == y.data: Both values are common, so we add either node to the output. Then we move forward in both linked lists.
- If x.data > y.data: The node with value y cannot be present in list1. In other words, the values of the nodes after x in list1 will be greater than y. So, we move forward in list2.
- If x.data < y.data: The node with value x cannot be present in list2. In other words, the values of the nodes after y in list2 will already be greater than x. So, we move forward in the list1.
Solution pseudocode
listIntersection(list1, list2)
intersecHead = NULL
intersecTail = NULL
while list1 != NULL AND list2 != NULL
if list1 -> data == list2 -> data
if intersecHead == NULL
intersecHead = new ListNode(list1 -> data)
intersecTail = intersecHead
else
intersecTail -> next = new ListNode(list1 -> data)
intersecTail = intersecTail -> next
list1 = list1 -> next
list2 = list2 -> next
else if list1 -> data < list2 -> data
list1 = list1 -> next
else
list2 = list2 -> next
return intersecHeadSolution analysis
In the worst-case scenario, we traverse each node of both linked lists at least once and perform an O(1) operation. If the length of both linked lists is m and n, then time complexity = (m + n) * O(1) = O(m + n). Space complexity = O(1), as we are using a constant number of variables.
Two pointers approach using dummy node
Solution idea
This strategy is similar to the previous approach, but it uses a temporary dummy node to simplify building the output list. We also maintain an intersectionTail pointer to the last node in that list, making it easy to append new nodes. Initially, intersectionTail points to the dummy node. Once the list is built, dummy.next points to its head, which we return.
A dummy node provides a fixed starting point for the output list. This makes it easy to append the first matching node and all subsequent nodes using the same steps, without needing a special case to initialise the list’s head. In other words, a dummy node is useful when building or modifying a linked list, especially when the head might change.
Solution pseudocode
listIntersection(list1, list2)
dummy = new ListNode(0)
intersecTail = dummy
while list1 != NULL AND list2 != NULL
if list1 -> data == list2 -> data
intersecTail -> next = new ListNode(list1 -> data)
intersecTail = intersecTail -> next
list1 = list1 -> next
list2 = list2 -> next
else if list1 -> data < list2 -> data
list1 = list1 -> next
else
list2 = list2 -> next
return dummy -> nextSolution analysis
We are using the loop idea similar to the above approach. So the time and space complexity will be the same in the worst-case. Time complexity = O(m + n) and Space complexity = O(1).
Using recursive approach
Solution idea and steps
Can we solve the problem using the solution of the smaller problem? Let's think! Suppose initial function call is listIntersection(list1, list2), then there will be three scenarios to build the recursive solution:
- If (list1.data = list2.data): Head nodes are common to both lists, and we add any one of the nodes to the intersection. Now, the problem gets reduced to find the intersection of the remaining list of sizes m - 1 and n - 1. So our function call for smaller sub-problem is listIntersection(list1.next, list2.next).
- If (list1.data < list2.data): The head node list1 will never be part of the intersection because data stored in all the nodes in the second linked list would be greater than the list1->data (hint: both lists are sorted!). Now, the problem gets reduced to find the intersection of the remaining sizes m - 1 and n. So our function call for smaller sub-problem is listIntersection(list1.next, list2).
- If (list1.data > list2.data): The head node list2 will never be part of the intersection because data stored in all the nodes in the first linked list will be greater than the list2->data. Now, the problem gets reduced to find the intersection of the remaining list of size m and n - 1. So our function call for smaller sub-problem is listIntersection(list1, list2.next).
Base case: if (list1 == NULL || list2 == NULL), return NULL.
Solution pseudocode
listIntersection(list1, list2)
if list1 == NULL OR list2 == NULL
return NULL
if list1 -> data < list2 -> data
return listIntersection(list1 -> next, list2)
else if list1 -> data > list2 -> data
return listIntersection(list1, list2 -> next)
else
temp = new ListNode(list1 -> data)
temp -> next = listIntersection(list1 -> next, list2 -> next)
return tempSolution analysis
At each step of the recursion, we are performing O(1) operation and input size decreases by 1 or 2. Suppose the length of both linked lists are m and n, then the total number of recursive calls would be m + n in the worst case. So time complexity = O(m + n) * O(1) = O(m + n).
Here is the recurrence relation.
- T(m, n) = T(m, n - 1) + O(1), if (list1->data > list2->data).
- T(m, n) = T(m - 1, n) + O(1), if (list1->data < list2->data).
- T(m, n) = T(m - 1, n - 1) + O(1), if (list1->data == list2->data)
The space complexity depends on the size of the recursion call stack, which depends on the height of the recursion tree. Similarly, the height of the recursion tree depends on the value of m and n i.e. whichever value is larger, recursion tree depth depends on that value. So space complexity = O((max(m , n)). Think!
Critical ideas to think!
- What would be the advantages of having doubly-linked lists instead of singly-linked lists for this problem?
- What would be the worst and best scenario of the above approaches?
- How can we solve this problem when linked lists are not sorted?
- Which of the above approach is more efficient in terms of memory or pointer operations? Compare the efficiency of the first three approaches.
- In the solution using local reference, can we build the output list by adding a new element to the front of the linked list?
Similar coding questions to practice
- Union and Intersection of two Linked Lists
- Find the intersection point of two Linked Lists without finding the length
- Merge two sorted linked lists
- Sorted merge of two sorted doubly circular linked lists
- Insert value in a sorted way in a sorted doubly linked list
Please write in the message below if you find anything incorrect, or you want to share more insight. Enjoy learning, Enjoy algorithms!
