Insertion Sort List

来源:互联网 发布:神经网络算法原理 编辑:程序博客网 时间:2024/06/05 17:38

Sort a linked list using insertion sort.

public ListNode insertionSortList(ListNode head) {        if(head == null){            return head;        }        ListNode helper = new ListNode(0);        ListNode cur = head;        ListNode next = null;        ListNode pre = helper;                while(cur != null){            next = cur.next;            while(pre.next != null && pre.next.val < cur.val){                pre = pre.next;            }            cur.next = pre.next;            pre.next = cur;            pre = helper;            cur = next;        }        return helper.next;    }


0 0
原创粉丝点击