登录
  • #刷题
  • #leetcode

14‌‌‍‍‌‍‍‌‍‍‌‌‌‌‌‍‌‍‌‌‌‍‌‌‍‍‍‌‌‌‍‌7. Insertion Sort List

小水
466
7
请教一下,为什么ListNode pre = res;这行必须放在大while loop之内

[mw_shl_code=java,true]public ListNode insertionSortList(ListNode head) {

if (head == null || head.next == null) return head;

ListNode res = new ListNode(-1);

ListNode cur = head;

while(cur != null){

ListNode pre = res;

ListNode next = cur.next;

while(pre.next != null && pre.next.val <= cur.val){

pre = pre.next;

}

//otherwise insertion (swap)

cur.next = pre.next;

pre.next = cur;

cur = next;

}

return res.next;

}[/mw_shl_code]
7条回复
热度排序

发表回复