[leetcode] 147. Insertion Sort List (Medium)

it2025-11-12  5

原题

别人的思路 非常简洁

function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * @return {ListNode} */ var insertionSortList = function(head) { if (head === null) { return head; } var helper = new ListNode(0); var cur = head.next; var pre = helper; var next = null; 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; };

转载于:https://www.cnblogs.com/ruoh3kou/p/9893431.html

最新回复(0)