
LeetCode 147 Insertion Sort List 题解
1.题目:
Sort a linked list using insertion sort.
题意:
用插入排序排序一个链表
2.解题思路:
3.代码
[title] [] [url] [link text]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
public class { public ListNode insertionSortList(ListNode head) { if (head == null||head.next==null) { return head; } ListNode ans = new ListNode(Integer.MIN_VALUE); ListNode node = head; while (node!=null) { ListNode nextListNode =node.next; ListNode inListNode =ans; while (inListNode.next!=null) { if (inListNode.next.val>node.val) break; inListNode=inListNode.next; } node.next=inListNode.next; inListNode.next=node; node=nextListNode; } return ans.next; } }
|
4.一些总结:
近期评论