edited by
1,137 views
1 votes
1 votes

What does the following program do on two linked lists?

 

Struct node  *myFun (struct node  * a, struct node * b)

{

Struct node   *new = NULL ;

If (a = = NULL) return (b) ;

if (b = = NULL) return (a) ;

If (a → data <= b → data)

{

new = a ;

new → next = myFun (a → next, b) ;

return (new);

}

else

{

new = b ;

new → next = myFun (a, b → next) ;

return (new) ;

}

}

if options are 

  • merges two unsorted linked lists

  • merges two linked lists by selecting the alternate nodes

  • merges two sorted linked lists into final sorted linked list

  • merges two linked lists by selecting the nodes in reverse.

edited by

3 Answers

4 votes
4 votes

The Program is basically merging two linked list in such a way that the start of the merged list becomes the smaller start from the given list.. 

The output varies from case to case.. 

case 1-

first list-  1->2->3->4

second list - 5->6->7

output - 1->2->3->4->5->6->-7

 

case 2-

first list - 3->2

second list-1->4

output - 1->3->2->4

case 3- 

first list- 4->2

second list- 1->3

output - 1->3->4->2

Hope you got the point.. 

please comment if there is anything wrong!! 

1 votes
1 votes
  • merges two sorted linked lists into final sorted linked list

1 votes
1 votes
it is basically comparing elements of one list with other list and which ever is smaller it puts in a final list so yes if both the lists are sorted then it is basically combining two sorted lists and producing sorted list.

Related questions

0 votes
0 votes
1 answer
1
kd..... asked Feb 9, 2018
2,026 views
the sorage requirements of a linked stack with n elements will be what
1 votes
1 votes
1 answer
2
kd..... asked Dec 30, 2017
969 views
In a circular single linked list how many external pointers are there because in some books there are two external pointers start pointing at first node and last pointing...