147. Insertion Sort List
来源:互联网 发布:湖北广电网络宽带资费 编辑:程序博客网 时间:2024/04/24 17:52
Sort a linked list using insertion sort.
不需要额外空间,只需要一个辅助头节点,遍历链表,保存当前节点的后继节点,找到当前节点应该插入到新链表的什么位置,在新链表里做插入操作,再取原来保存的后继节点继续遍历下去。
public class Solution { public static ListNode insertionSortList(ListNode head){if (head == null || head.next == null)return head;ListNode dummy = new ListNode(Integer.MIN_VALUE);ListNode node = head;while (node != null){ListNode find = dummy;while (find.next != null && find.next.val < node.val)find = find.next;ListNode nodenext=node.next;node.next=find.next;find.next=node;node=nodenext;}return dummy.next;}}
0 0
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 【poj1995】Raising Modulo Numbers
- QUSTOJ1782 A奶牛的锻炼(线性动归)
- POJ 2393 Yogurt factory (贪心)
- eclipse error resource does not exist
- 147. Insertion Sort List
- SCPPO(十):网站发布中的问题锦集—手动发布网站
- uva 11889 Benefit
- PAT--1027. Colors in Mars
- CodeForces 698A Vacations
- Android Studio下使用Android Fragment的简单Demo(1)
- UVALive 3266 Tian Ji -- The Horse Racing
- HDU - 5363 Key Set
- 三、类型、值、变量