/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return ListNode类
*/
function insertionSortList( head ) {
// write code here
if(head==null || head.next==null) {
return head;
}
var preHead = new ListNode(-1);
var curr = head;
while(curr){
var pre = preHead;
var next = curr.next;
while(pre.next && pre.next.val < curr.val){
pre = pre.next
}
curr.next = pre.next;
pre.next = curr;
curr = next;
}
return preHead.next;
}
module.exports = {
insertionSortList : insertionSortList
};
(排序)链表的插入排序
最新推荐文章于 2022-01-02 23:15:51 发布