Question: In java, implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as

In java, implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program.
HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break up the list into two, null terminated lists, based on the node count.
The mergesort function should accept only a single linked list and it should return a linked list. The merge function should take 2 linked lists and also return a linked list. The look of the function will be very similar to the array version of the mergesort algorithm. You should not use any of the built-in classes or functions. You have to create your linked list.
//Here is a portion of what mergeSort function would look like:
//a points to the left partition, b points to the right partition. They would be passed by reference.
//You would then recursively process each partition.
if(head==NULL)
return NULL;
if (head->next==NULL)
return head;
a=mergeSort(a);
b=mergeSort(b);
c=merge(a,b);
return (c);
//These are the function headers
void split (node* head,node*&a,node*&b)
node* merge(node* a, node* b)
node* mergeSort( node* head)
Make sure to account for when head is null or when there is only one item in the list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!