[leetcode]Insertion Sort List
2014-07-21 14:50
399 查看
Insertion Sort List
Sort a linked list using insertion sort.
算法思路:
最基本的插入排序,时间复杂度O(n*n),空间复杂度O(1)
代码:
Sort a linked list using insertion sort.
算法思路:
最基本的插入排序,时间复杂度O(n*n),空间复杂度O(1)
代码:
public class Solution { public ListNode insertionSortList(ListNode head) { if(head == null || head.next == null) return head; ListNode list = new ListNode(0); list.next = head; head = head.next; list.next.next = null; while(head != null){ ListNode tail = list; ListNode node = head; head = head.next; node.next = null; while(tail != null){ if(tail.next == null){ tail.next = node; break; } if(tail.next.val > node.val){ node.next = tail.next; tail.next = node; break; }else{ tail = tail.next; } } } return list.next; } }
相关文章推荐
- Leetcode: Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- Leetcode Insertion Sort List
- LeetCode: Insertion Sort List
- LeetCode147 Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode - Insertion Sort List
- Leetcode 147 Insertion Sort List
- [leetcode] Insertion Sort List
- leetcode 147: Insertion Sort List
- [LeetCode] Insertion Sort List
- LeetCode 之 Insertion Sort List — C++ 实现
- leetcode - Insertion Sort List
- LeetCode----Insertion Sort List
- LeetCode | Insertion Sort List
- leetCode(4):Insertion Sort List
- LeetCode-M-Insertion Sort List
- Leetcode: Insertion Sort List
- [leetcode] Insertion Sort List
- LeetCode 147 Insertion Sort List