leetcode_Sort a linked list using insertion sort.
2014-05-13 10:27
323 查看
/* * Sort a linked list using insertion sort. * */ public ListNode insertionSortList(ListNode head) { ListNode p = new ListNode(0); ListNode phead = p; while(head!=null) { p = phead; //reset p to the phead while(p.next!=null) { if(p.next.val>head.val) { ListNode tmp = p.next; p.next = head; head = head.next; //here head next p.next.next = tmp; break; } p = p.next; } if(p.next == null) { p.next = head; head = head.next;//here head next p.next.next = null; } } return phead.next; } //插入排序总结延伸:基于链式存储的插入排序,前面有一个题目要求O(nlogn)时间复杂度和O(1)额外空间实现,利用二路归并递归实现。
相关文章推荐
- leetcode上的Sort a linked list using insertion sort
- [LeetCode] 148. Sort List (Linked List) - Using Quick Sort(小改动)
- Sort a linked list using insertion sort
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list using insertion sort
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- [LeetCode] Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List (LeetCode)
- LeetCode Insertion Sort List 链表的插入排序
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- [Leetcode] Insertion Sort List
- leetcode Insertion Sort List(链表插入排序)
- Insertion Sort List -- leetcode
- Leetcode[147]-Insertion Sort List
- Leetcode_Insertion Sort List
- leetcode_num179_Insertion Sort list
- LeetCode Insertion Sort List 链表插入排序
- leetcode Insertion Sort List
- leetcode -- Insertion Sort List