answersLogoWhite

0


Best Answer

Here is the java version, C should be similar

// merges two Singly linked lists A and B, both sorted in increasing order of integers .

// Return a Singly linked list sorted in increasing order

// for ex if A = 1->2->3->6 and B = 2->4->7, returns 1->2->2->3->4->6->7

public LinkedList merge(LinkedList A, LinkedList B) {

Node 1A = A.head;

Node 1B = B.head;

Node P1A = NULL;

Node 2A = NULL;

Node 2B = NULL;

while ( 1A != NULL && 1B !=NULL){

2A = 1A.next;

2B = 1B.next;

if (1A.item >= 1B.item) {

if (P1A != NULL) {

P1A.next = 1B;

}

else if ( P1A == NULL){

P1A = 1B;

}

1B.next = 1A;

P1A = 1B;

1B = 2B;

}

else if ( 1A.item < 1B.item){

if ( 2A != NULL) {

P1A = 1A;

1A = 1A.next;

}

else{

1A.next = 1B;

break;

}

}

}

return A;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to merge two single linked list using c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


How would you sort a linked list?

Use merge sortUse tree sort


Which is the easy insertion operator single linked-list or double-linked list?

It is easier to insert into a singly linked list.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


Which operation is perform more efficiently by doubly linked list than by single linked list?

zsd


Can a linked list implemented using an array be accessed by giving the location?

A linked list implemented with an array defeats the purpose of using a linked list, which is to address the memory allocation problems associated with arrays.


How to write aC program to merge two singly linked list?

write a c program to circular queue


Where linked list using?

linked list are used for creation of stack,queues to use memory in optimum manner linked list are used as they are dynamic in nature


Why double linked list better than single link list?

In_which_way_Doubly_linked_list_better_than_singly_linked_list


Differences linked-list non contiguous allocation and linked-list allocation using index?

ok


What are the linked list utility in memory management?

A simple linked-list is an approach is to store the freelist of memory blocks, where each node of the linked list contains a pointer to a single free block in the memory pool.