Skip to main content

Merge point of linked lists

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.
  • 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

Popular posts from this blog

In order traversal of nodes in the range x to y

Question : Write a function for in-order traversal of nodes in the range x to y from a binary search tree. This is quite a simple function. As a first solution we can just traverse our binary search tree in inorder and display only the nodes which are in the range x to y. But if the current node has a value less than x, do we have to traverse its left subtree? No. Because all the nodes in left subtree will be smaller than x. Similarly if the current node has a key value more than y, we need not visit its right subtree. Now we are ready to write our algorithm.     if nd is NOT NULL  if nd->val >=x then visit all the nodes of left subtree of nd recursively display nd->val if nd->val <y then visit all the nodes of right subtree of nd recursively  That's all. We have our function ready. void in_order_middle (NODEPTR nd, int x, int y) { if (nd) { if (nd -> val >= x) in_order_middle(nd...

Delete a node from doubly linked list

Deletion operation in DLL is simpler when compared to SLL. Because we don't have to go in search of previous node of to-be-deleted node.  Here is how you delete a node Link previous node of node of to-be-deleted to next node. Link next node of node of to-be-deleted to previous node. Free the memory of node of to-be-deleted Simple, isn't it. The code can go like this. prevnode = delnode->prev; nextnode = delnode->next; prevnode->next = nextnode; nextnode->prev = prevnode; free(delnode); And that is it. The node delnode is deleted. But we should always consider boundary conditions. What happens if we are trying to delete the first node or last node? If first node is to be deleted, its previous node is NULL. Hence step 3 should not be used.  And also, once head is deleted, nextnode becomes head . Similarly if last node is to be deleted, nextnode is NULL. Hence step 4 is as strict NO NO. And we should set prevnode to tail. After we put these things together, we have...

Josephus problem

Question: Write a function to delete every k th node from circular linked list until only one node is left. This has a story associated with it. Flavius Josephus was Jewish Historian from 1st century. He and 40 other soldiers were trapped in a cave by Romans. They decided to kill themselves rather than surrendering to Romans. Their method was like this. All the soldiers will stand in a circle and every k th soldier will be shot dead. Josephus said to have calculated the starting point so that he would remain alive. So we have similar problem at hand. We delete every kth node in a circular list. Eventually only one node will be left. e.g. Let us say this is our list And we are deleting every third node.  We will delete 30. Then we delete 60. Next we delete 10. Next it will be 50. Next to be deleted is 20. Next 80. This continues. Implementation   We can count k-1 nodes and delete next node. This can be repeated in  a loop. What must be the termina...