Can anyone tell me what's wrong with my code?


  • 0
    S

    I got MLE.
    I have tested in eclipse, I found a circle, but I don't know how I get this circle. Somebody help, please.

     /**
         * Definition for singly-linked list.
         * public class ListNode {
         *     int val;
         *     ListNode next;
         *     ListNode(int x) { val = x; }
         * }
         */
        public class Solution {
            public ListNode sortList(ListNode head) {
                if(head == null) return head;
                ListNode it = head;
                while(it.next!=null){
                    it = it.next;
                }
                return sortList(head, it);
            }
            private ListNode sortList(ListNode s, ListNode e){
                if(s == e || e == null || s == null) return s;
                ListNode pivot = e;
                ListNode it = s;
                ListNode pre = null;
                while(it != pivot){
                    if(it.val >= pivot.val){
                        if(it == s){
                            s = it.next;
                            it.next = null;
                            e.next = it;
                            e = it;
                            it = s;
                        }
                        else{
                            pre.next = it.next;
                            it.next = null;
                            e.next = it;
                            e = it;
                            it = pre.next;
                        }
                    }
                    else{
                        pre = it;
                        it = it.next;
                    }
                }
                ListNode head = sortList(s, pre);
                it = head;
                while(it.next!=null)
                    it = it.next;
                it.next = pivot;
                pivot.next = sortList(pivot.next, e);
                return head;
            }
        }

  • 0
    S

    public class Solution {
    public ListNode sortList(ListNode head) {
    if(head == null) return head;
    ListNode it = head;
    while(it.next!=null){
    it = it.next;
    }
    return sortList(head, it);
    }
    private ListNode sortList(ListNode s, ListNode e){
    if(s == e || s == null) return s;
    e.next = null;
    ListNode pivot = e;
    ListNode it = s;
    ListNode pre = null;
    while(it != pivot){
    if(it.val >= pivot.val){
    if(it == s){
    s = it.next;
    it.next = null;
    e.next = it;
    e = it;
    it = s;
    }
    else{
    pre.next = it.next;
    it.next = null;
    e.next = it;
    e = it;
    it = pre.next;
    }

            }
            else{
                pre = it;
                it = it.next;
            }
        }
        ListNode head = null;
        if(s != pivot){
            head = sortList(s, pre);
            it = head;
            while(it.next!=null)
                it = it.next;
            it.next = pivot;
        }else
            head = pivot;
        pivot.next = sortList(pivot.next, e);
        return head;
    }
    

    }
    I have changed the code to this, and it can get result in Eclipse, but still got MLE when I submit here. Help, please.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.