My quick sort algorithm is right, but why it is time limit exceeded


  • 0
    U

    public class Solution {

    public ListNode sortList(ListNode head) {
         return sortLinkList(head,null);
    }
    ListNode sortLinkList(ListNode head,ListNode tail)
    {
       
        if(head==tail||head==null)
          return head;
         else
         {
            ListNode point=head;
            ListNode nextTail=head;
            int val=head.val;
            ListNode temp;
            while(point.next!=tail)
            {
                if(point.next.val<val)
                {
                    temp=point.next;
                    point.next=temp.next;
                    temp.next=head;
                    head=temp;
                }
                else
                {
                    point=point.next;
                }
            }
            head=sortLinkList(head,nextTail);
            nextTail.next=sortLinkList(nextTail.next,tail);
            return head;
         }
    }
    

    }


Log in to reply
 

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