Write a 'C' Function to combine two singly connected linked lists in the following manner. Suppose one list is "L" which can be given by L=(l0,l1,.....,lm) and the other list is "M" where "M" can be expressed as M=(m0,m1,......,mn) where each li,mi represents one node in the respective lists. For simplicity, you may assume that each node contains an integer as the data. After combining them the combined list should be (l0,m0,l1,m1,.....,). Do not Use any additional node for writing the Function...

Please give some idea about Implementing this Function...!!

If you know how to implement a normal linked list, this one should be no problem at all. What is the challenge?? Start with a node from L, l->next is the address of a node from M. M-> next is address of next node of L. So on and so forth.

My code for this function is:

node * combine(node *l,node *m){
       node *p,*q;
       if(l==NULL)
	  p=m;
       else{
	  q=l;
	  p=q; // storing first node address in p
	  l=l->next;
	  while(m!=NULL){
	    q->next=m;
	    m=m->next;
	    q=q->next;
	    if(l==NULL){
	      q->next=m;
	      break;
	    }
	   else{
	      q->next=l;
	      q=q->next;
	      l=l->next;
	   }
	 }
       }
   return p;
  }

But I have to use Additional nodes p and q in this Function... What should i need to do for making it without additional nodes...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.