Leetcode习题:Insertion Sort List
2013-12-25 22:52
441 查看
Insertion Sort List
Total Accepted: 2832 TotalSubmissions: 11108My Submissions
Sort a linked list using insertion sort.
public class Solution { public ListNode insertionSortList(ListNode head) { if (head == null) { return null; } ListNode p1 = head.next; head.next = null; while (p1 != null) { ListNode prev = null; ListNode p2 = head; while (p2!=null && p2 != p1 && p2.val < p1.val) { prev = p2; p2 = p2.next; } ListNode next = p1.next; if(p2 == null){ prev.next = p1; p1.next = null; }else if (prev == null) {// head p1.next = head; head = p1; }else { p1.next = prev.next; prev.next = p1; } p1 = next; } return head; } }
相关文章推荐
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode NO.147 Insertion Sort List
- LeetCode :Insertion Sort List
- LeetCode OJ Insertion Sort List
- LeetCode题目5: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
- leetcode day2 -- Sort List && Insertion Sort List
- [LeetCode]Insertion Sort List
- 59_leetcode_Insertion Sort List
- LeetCode -- Insertion Sort List
- LeetCode_Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode 147 Insertion Sort List
- [LeetCode] Insertion Sort List 链表插入排序
- leetcode Insertion Sort List
- 【leetcode】Insertion Sort List