Question:
How do you find the merge point of two linked lists?
Merge point is the first common node of two linked lists. From this node onwards, the lists have same nodes.
Note: we are not talking about values in the nodes, but the actual addresses themselves.
In the diagram shown, 4 is the merge point of the two lists.
You may think that easy solution is traverse two list parallely and compare the respective nodes of the lists. But that does not work.
In the diagram shown, if you start with head of list1 and list2, if you compare addresses of 1 and 5, then addresses of 2 and 6, addresses of 3 and 4 and so on, you will never find a match.
An easy solution would be to take one node at a time from first list and compare this with all nodes in second list. Then move on to second node of first list, compare it with all nodes of second list and so on. And continue this process until a match is found.
But this process has a complexity of O(n2).
Another method will be to find the lengths of two lists - say l1 and l2 and then find their difference (l1-l2). Then traverse l1-l2 nodes from the longer list. Now we have the points of equal lengths in two lists.
Next we can traverse paralelly and compare the nodes.
Here is our function.
We are finding the lengths of lists l1 and l2. If l1 is greater we are moving d (difference between lengths) nodes in l1. If not we are moving d noes in l2.
Next we compare l1 to l2. If they are unequal, both are set to next nodes. When l1 is equal to l2, we have found our point of merger of two lists.
If there is no such node, we return NULL.
And here you find two helper functions - skip_nodes() and find_length()
In fact, most lists we find do not have a merge point. :)
To test this function, we need to explicitly merge two lists using some tweak.
You can find the driver program for this here.
How do you find the merge point of two linked lists?
Merge point is the first common node of two linked lists. From this node onwards, the lists have same nodes.
Note: we are not talking about values in the nodes, but the actual addresses themselves.
In the diagram shown, 4 is the merge point of the two lists.
You may think that easy solution is traverse two list parallely and compare the respective nodes of the lists. But that does not work.
In the diagram shown, if you start with head of list1 and list2, if you compare addresses of 1 and 5, then addresses of 2 and 6, addresses of 3 and 4 and so on, you will never find a match.
An easy solution would be to take one node at a time from first list and compare this with all nodes in second list. Then move on to second node of first list, compare it with all nodes of second list and so on. And continue this process until a match is found.
But this process has a complexity of O(n2).
Another method will be to find the lengths of two lists - say l1 and l2 and then find their difference (l1-l2). Then traverse l1-l2 nodes from the longer list. Now we have the points of equal lengths in two lists.
Next we can traverse paralelly and compare the nodes.
- Find length of list - l1
- Find length of list - l2
- Find the difference - d = abs(l1-l2)
- Traverse d nodes from longer list (from first list if l1 is larger, from second list if l2 is larger)
- Repeat the loop as long as n1 and n2 are not null
- Compare nodes n1 of list1 and n2 of list2.
- If they are equal break the loop - this is merge point.
- move to next nodes -- n1 = n1->next and n2 = n2->next
Here is our function.
NODEPTR find_merge_point(NODEPTR l1,NODEPTR l2) { int len1 = find_length(l1); int len2 = find_length(l2); if(len1>len2) { l1 = skip_nodes(l1,len1-len2); }else{ l2 = skip_nodes(l2,len2-len1); } while(l1 && l2) { if(l1==l2) return l1; l1 = l1->next; l2 = l2->next; } return NULL; }
We are finding the lengths of lists l1 and l2. If l1 is greater we are moving d (difference between lengths) nodes in l1. If not we are moving d noes in l2.
Next we compare l1 to l2. If they are unequal, both are set to next nodes. When l1 is equal to l2, we have found our point of merger of two lists.
If there is no such node, we return NULL.
And here you find two helper functions - skip_nodes() and find_length()
int find_length(NODEPTR head) { int c =0; while(head) { c++; head = head->next; } return c; } NODEPTR skip_nodes(NODEPTR head, int num) { while(num) { head = head->next; num--; } return head; }
In fact, most lists we find do not have a merge point. :)
To test this function, we need to explicitly merge two lists using some tweak.
You can find the driver program for this here.
Comments
Post a Comment