-
Sorting singly linked list which takes only one argument
over 8 years ago
-
over 8 years ago
I have a solution, not the fastest one, but it respect the conditions. Imagine your list is :
class LinkedInt { int integer; LinkedInt next; LinkedInt(int integer) { this.integer = integer; } }
Then you can sort like this :
void sort(LinkedInt linkedInt) { if (linkedInt == null || linkedInt.next == null) return; sort(linkedInt.next); LinkedInt current = linkedInt; int temp; while (current.next != null && current.integer > current.next.integer) { temp = current.integer; current.integer = current.next.integer; current.next.integer = temp; current = current.next; } }
It sorts, it is recursive, it have only one parameter and not depends on other method. The idea : we sort the queue first, then since queue is sorted, it is easy to push the value to the good place. Hope it's Help JHelp
-
1 Answer(s)