0

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

Edited by Nick Evan: Removed obnoxious bold and color-tags

2
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by vinitmittal2008
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.