Accepted Solution Java


  • 0
    P

    public class Solution {
    public ListNode insertionSortList(ListNode l1) {
    if(l1 == null || l1.next == null) return l1;
    ListNode head=l1, sortTail = head, prev = null, curr = sortTail.next;

        while(curr != null){
            l1 = head;
            prev = null;
            if(sortTail.val > curr.val){
                while(l1.val < curr.val){
                    prev = l1;
                    l1=l1.next;
                }
                sortTail.next = curr.next;
                curr.next = l1;
                if(prev != null) prev.next = curr;
                else head = curr;
                curr = sortTail.next;
            }else{
                sortTail = curr;
                curr = curr.next;
            }
        }
        return head;
    }
    

    }


Log in to reply
 

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