I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one:
LeetCode.
My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.
//defining a job that has priority
// defining a linked list of elements
//recursive merge sort - this consists of two methods, the first that recursively breaks the linked list into half and half and so on, and the second, which compares the priorities of the individual jobs in nodes.
The problem I've been trying to solve is that I am getting the stack overflow at line
ListNode<T> h1 = mergeSort(left);
meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on. I would be grateful if you can point me to the right direction, because I've double checked everything, and right now I am not sure to see where the error might be. Thanks for any input.
This post loosely follows the previous post on BMS
BSM - linked lists