java - Sort a linked list in O(n log n) with O(1) space complexity -


This is a sorted list problem on LeetCode I have created a Java solution but due to this, the time limit on a very long test case has occurred. And I did not find a bug in my code. Can anyone tell where the bug is? thanks a lot.

class ListNode {int val; List node next; ListNode (int x) {val = x; Next = null; }} Public Class Solutions {Public Listord Sartelist (List Needed Head) {Return this sorting (head, blank); } Private ListNode sortpart (ListNode start, ListNode end) {if (start == null) return null; If start (start == end) start; ListNode l = start, r = start, p = start.next; While (p! = Null & amp; p! = End) {if (p.val .. & lt; start.val) {// Current node is less than the start node. Nnx = p.next; P.next = l; L = p; // Set the current node as left p = start.next; // go to the next node} and {// the current node does not start node R = P; // Set the current node as equal and go to next node P = p.next; }} // recursive sort left part and right side sort part (L, start); Sortpart (start.next, r); Returns L; }}

The bug is probably that a QuickTest O (n * n) . A practical solution is the random selection of the spindle. The standard online reservoir sample algorithm fixes it.

However this may still not be enough. Quicksort will create a callstock with the o (log) code which takes O (log). If they establish good tests, they will be able to verify that you are finished.

For the real solution, see.

Given that how many people have overcome this problem, I suspect that they have O (log (n)) space and o (1) / Code> Do not catch the difference between space correctly.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -