您的位置:首页 > 其它

LinkedList-147-Insertion Sort List

2018-02-01 21:14 239 查看
Description:

Sort a linked list using insertion sort.

Solution:

class Solution {
public ListNode insertionSortList(ListNode head) {
if(head == null) return null;
ListNode cur = head;
ListNode result = new ListNode(0);
ListNode helper = result;
ListNode next = null;
while(cur != null){
next = cur.next;
if(helper.next == null || helper.next.val > cur.val) helper = result;
while(helper.next != null && helper.next.val <cur.val) helper = helper.next;
cur.next = helper.next;
helper.next = cur;
cur = next;
}
return result.next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: