Easy to understand java solution


  • 0
    C
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if(head==null || head.next ==null) return head;
            ListNode prev = new ListNode(-1);
            ListNode dummy = prev;
            prev.next = new ListNode(head.val);
            ListNode next = head.next;
            while(next!=null){
                ListNode curr = next;
                while(prev.next!=null && prev.next.val<=curr.val){
                    prev = prev.next;
                }
                ListNode temp = curr.next;
                curr.next = prev.next;
                prev.next = curr;
                prev = dummy;
                next = temp;
            }
            return dummy.next;
        }
    }
    

Log in to reply
 

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