[leetcode] Insertion Sort List(python)
2016-03-02 20:50
731 查看
简单的插入排序,总是超时,暂且放在这记录一下。
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; }
相关文章推荐
- 基于Python Django技术构建web系统实践
- python脚本:计算某个目录下Code行数
- python学习小记0
- Python练习(4):牛顿拉复生算法求解根
- 使用Python和Perl绘制北京跑步地图
- python的命令模式和编辑模式
- 和python库有关的网址
- python---matplotlib安装、networkx的使用
- hello,world! python
- flask源码笔记:四,总结
- vim+python+OpenCV学习五 : 直方图的计算与显示
- python学习1
- flask源码笔记:二,Flask源码目录结构
- Python基础回顾之-格式化字符串
- flask源码笔记:一,阅读Flask源码前的准备工作
- Ipython
- Python学习记录(六)
- python多线程threading.Lock锁实例
- python 控制vbox虚拟机
- Python设计模式中单例模式的实现及在Tornado中的应用