Can anyone help me to find the "StackOverFlowError" of my answer?


  • 1
    H
    public ListNode sortList(ListNode head) {
    	if(head==null){ return null;}
    	if(head.next==null){ return head;}
    	ListNode mid = getMid(head);
    	ListNode fharf =head;
    	ListNode sharf=mid.next;
    	return merge2Lists(sortList(fharf),sortList(sharf));
    }
    
    public ListNode getMid(ListNode head){
    	if(head==null) {return head;}
    	if(head.next==null) return head;
    
    	ListNode slow=head;
    	ListNode fast = head;
    	while(slow.next!=null&&fast.next.next!=null){
    		slow=slow.next;
    		fast=fast.next.next;
    	}
    	return slow;
    }
    
     public ListNode merge2Lists(ListNode a, ListNode b) {
        ListNode dummyHead, curr; 
        dummyHead=null;
        curr = dummyHead;
        while(a !=null && b!= null) {
            if(a.val <= b.val) { curr.next = a; a = a.next; }
            else { curr.next = b; b = b.next; }
            curr = curr.next;
        }
        curr.next = (a == null) ? b : a;
        return dummyHead.next;
    }

  • 0
    W

    I don't understand why this question is closed. Basically the po got StackOverFlowError after submitting his/her code and wanted to know where the unnecessary space usage come from.


Log in to reply
 

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