[leetcode] Insertion Sort List(python)
2014-07-05 15:29
381 查看
简单的插入排序,总是超时,暂且放在这记录一下。
c++AC的
class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if head == None or head.next == None: return head psuhead = ListNode(-1) while head: tail = psuhead headnext = head.next while tail.next and tail.next.val < head.val: tail = tail.next head.next = tail.next tail.next = head head = headnext return psuhead.next
Last executed input: | {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,2 |
ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; //排序涉及到头结点的变化,为了方便,我们定义一个额外的伪头结点 ListNode *psuHead = new ListNode(-1); while(head){ ListNode *pre = psuHead; ListNode *tail = psuHead->next; while(tail && tail->val < head->val){ pre = tail; tail = tail->next; } ListNode *nextHead = head->next; head->next = pre->next; pre->next = head; head = nextHead; } head = psuHead->next; delete (psuHead); return head; }
相关文章推荐
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- [LeetCode]题解(python):147-Insertion Sort List
- LeetCode-Insertion Sort List (Python)
- [leetcode]Insertion Sort List @ Python
- leetcode 【 Insertion Sort List 】 python 实现
- 【LeetCode with Python】 Insertion Sort List
- [Leetcode][python]Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- Insertion Sort List Leetcode Python
- [leetcode] Insertion Sort List(python)
- leetcode - Insertion Sort List
- leetcode 133: Insertion Sort List
- Leetcode Insertion Sort List 解题报告
- Insertion Sort List | leetcode
- [Leetcode] Insertion Sort List (Java)
- LeetCode | Insertion Sort List
- Leetcode: Insertion Sort List
- [leetcode]Insertion Sort List
- LeetCode - Insertion Sort List
- LeetCode:Insertion Sort List