lintcode 173 单链表插入排序 python
来源:互联网 发布:mac怎么恢复废纸篓 编辑:程序博客网 时间:2024/06/07 05:04
"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""class Solution: """ @param head: The first node of linked list. @return: The head of linked list. """ def insertionSortList(self, head): # write your code here if head.next is None:return head prehead = head #定义temp为拟排序节点 temp = head.next if temp is not None:prenext = temp.next else:prenext = None flag = 0 #标记是否对temp做了排序 while temp: nowhead = head #定义nownode 为对比节点 nownode = head.next if temp.val < head.val: #首先对比head的值进行判断 temp.next = head head = temp temp = prenext prehead.next = prenext if temp is not None:prenext = temp.next else:prenext = None continue while nownode != temp: #然后循环对比temp前的所有节点 if temp.val < nownode.val: flag = 1 nowhead.next = temp temp.next = nownode prehead.next = prenext temp = prenext if temp is not None:prenext = temp.next else:prenext = None break else: nowhead = nownode nownode = nownode.next if flag == 0: prehead = temp temp = prenext if temp is not None:prenext = temp.next else:prenext = None flag = 0 return head
阅读全文
0 0
- lintcode 173 单链表插入排序 python
- lintcode 173 链表插入排序
- LintCode 链表插入排序
- lintcode-链表插入排序
- LintCode-链表插入排序
- 链表插入排序 lintcode
- LintCode 链表插入排序
- 链表插入排序-LintCode
- LintCode 链表插入排序
- 链表插入排序-LintCode
- Python--排序--插入排序
- LintCode Python 合并排序数组
- LintCode(E) 链表插入排序
- Lintcode——链表插入排序
- lintcode python 容易 60.搜索插入位置
- 插入排序 python
- python插入排序算法
- python之插入排序
- Java8源码-Hashtable(2)
- Nginx + keepalived 实现高可用HA 【主从架构】
- 大数乘法
- 动态规划算法
- 同一服务器部署多个tomcat配置
- lintcode 173 单链表插入排序 python
- 用CSS3美化半个字符巧妙方法
- JavaScript中apply()、call()和bind()的相似与区别
- 根据函数名称导出函数
- sort排序
- 51nod 1597 有限背包计数问题 dp
- MongoDB的使用
- Lucene第三天入门
- 基于sklearn的文本特征提取与分类