Accepted recursive java solution


  • 1
    G
    public ListNode insertionSortList(ListNode head) {
        if(head==null || head.next==null) return head;
    
        head.next = insertionSortList(head.next);
        if(head.val<=head.next.val) return head;
        
        ListNode next = head.next;
        ListNode cur = head;
        while(cur.next!=null && head.val>cur.next.val) {
            cur = cur.next;
        }
        head.next = cur.next;
        cur.next = head;
        head = next;
        return head;
        
    }

Log in to reply
 

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