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

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

Program to delete a node from linked list

How do you remove a node from a linked list? If you have to delete a node, first you need to search the node. Then you should find its previous node. Then you should link the previous node to the next node. If node containing 8 has to be deleted, then n1 should be pointing to n2. Looks quite simple. Isn't it? But there are at least two special cases you have to consider. Of course, when the node is not found. If the node is first node of the list viz head. If the node to be deleted is head node, then if you delete, the list would be lost. You should avoid that and make the second node as the head node. So it becomes mandatory that you return the changed head node from the function.   Now let us have a look at the code. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; typedef struct node * NODEPTR; NODEPTR create_node ( int value) { NODEPTR temp = (NODEPTR) malloc( size...

Program to create a Linked List in C

An array is a commonly used data structure in most of the languages. Because it is simple, it needs O(1) time for accessing elements. It is also compact. But an array has a serious drawback - it can not grow or shrink. You need to estimate the array size and define it during compile time. This drawback is not present a linked list. A linked list is a data structure which can grow or shrink dynamically.  A linked list has nodes each of which contain  contain  data and a link to next node . These nodes are dynamically allocated structures. If you need more nodes, you just need to allocate memory for these and link these nodes to the existing list. The nodes of a linked list have to be defined as self-referential structures in C. That is structures with data members and one member which is a pointer to the structure of same type.  This pointer will work as a link to next node. struct node { int data; struct node * next; //pointer to another node }...