[LintCode]Insertion Sort List
来源:互联网 发布:练胸肌 知乎 编辑:程序博客网 时间:2024/05/17 03:32
Sort a linked list using insertion sort.
Example
Given 1->3->2->0->null
, return 0->1->2->3->null
.
1,使用dummy节点,便于处理插入位置为首位的问题。
2,1个head指针,指向待处理list首位。
3,1个临时变量,保存下一个head节点位置。
此题一个指针+一个临时变量即可。已排序list不需要和未排序list保持相连。
public class Solution { /** * @param head: The first node of linked list. * @return: The head of linked list. */ public ListNode insertionSortList(ListNode head) { ListNode dummy = new ListNode(0); while (head != null) { ListNode node = dummy; while (node.next != null && node.next.val < head.val) { node = node.next; } ListNode temp = head.next; head.next = node.next; node.next = head; head = temp; } return dummy.next; }}
0 0
- [LintCode]Insertion Sort List
- 【lintcode】——链表插入排序-Insertion Sort List
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- [LeeCode] Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Android - 如何获取手机内存
- Candies-POJ3159差分约束
- Andriod5.0以上APP打开白屏(无响应)
- FragmentManage详解
- Java/Android中四种引用类型
- [LintCode]Insertion Sort List
- 【React Native开发】React Native控件之WebView组件详解以及实例使用(22)
- python 数据挖掘基础 入门
- 313. Super Ugly Number LeetCode
- Linux实现的IEEE 802.1Q VLAN
- 成为优秀程序员的10条黄金法则
- 用HOG+SVM在Inria数据集上做行人检测与评测
- SVN的日常操作指南
- Android消息机制