leetcode--Insertion Sort List
2017-08-08 12:52
357 查看
Sort a linked list using insertion sort.
[java] view
plain copy
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null) return null;
ListNode p = new ListNode(Integer.MIN_VALUE);
ListNode cur = head;
ListNode t = p;
while(cur!=null){
ListNode n = cur.next;
t = p;
while(t.next!=null&&t.next.val<cur.val){
t = t.next;
}
cur.next = t.next;
t.next = cur;
cur = n;
}
return p.next;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/46564295
[java] view
plain copy
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null) return null;
ListNode p = new ListNode(Integer.MIN_VALUE);
ListNode cur = head;
ListNode t = p;
while(cur!=null){
ListNode n = cur.next;
t = p;
while(t.next!=null&&t.next.val<cur.val){
t = t.next;
}
cur.next = t.next;
t.next = cur;
cur = n;
}
return p.next;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/46564295
相关文章推荐
- leetcode---Insertion Sort List
- [Leetcode]Insertion Sort List
- [LeetCode] Insertion Sort List 单向链表插入排序
- LeetCode_Insertion Sort List
- LeetCode | Insertion Sort List(插入法排序链表)
- LeetCode Insertion Sort List
- leetcode - Insertion Sort List
- LeetCode Insertion Sort List
- leetcode_Sort a linked list using insertion sort.
- Insertion Sort List | leetcode
- 【LeetCode】-Insertion Sort List
- [leetcode]Insertion Sort List
- leetcode -- Insertion Sort List
- LeetCode_Insertion Sort List
- [leetcode][list][sort] Insertion Sort List
- [LeetCode]Insertion Sort List
- LeetCode: Insertion Sort List(外加对单链表类题目的思考总结)
- Leetcode:Insertion Sort List
- 【leetcode】Insertion Sort List
- leetcode insertionSortList