[Leetcode][python]Insertion Sort List
2017-12-16 09:49
453 查看
题目大意
通过插入排序的方法排序一个链表。解题思路
参考:http://www.cnblogs.com/zuoyuan/p/3700105.html![](http://images.cnitblog.com/i/627993/201404/292108480954088.jpg)
代码
class Solution(object): def insertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head dummy = ListNode(0) dummy.next = head curr = head while curr.next: if curr.next.val < curr.val: # 直到某数小于其前面的数,进入 pre = dummy # 回到头开始往后遍历 while pre.next.val < curr.next.val: # 用pre直到找到应该插入的位置 pre = pre.next # 如上图 tmp = curr.next curr.next = tmp.next tmp.next = pre.next pre.next = tmp else: curr = curr.next return dummy.next
总结
相关文章推荐
- [LeetCode]题解(python):147-Insertion Sort List
- [leetcode]Insertion Sort List @ Python
- Insertion Sort List Leetcode Python
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- 【LeetCode with Python】 Insertion Sort List
- leetcode 【 Insertion Sort List 】 python 实现
- [leetcode] Insertion Sort List(python)
- LeetCode-Insertion Sort List (Python)
- [leetcode] Insertion Sort List(python)
- 【LEETCODE】147- Insertion Sort List [Python]
- [leetcode]Insertion Sort List
- LeetCode | Insertion Sort List
- [Leetcode]Insertion Sort List
- Insertion Sort List (LeetCode)
- leetcode. Insertion Sort List
- [LeetCode] Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List Leetcode java
- LeetCode---Insertion Sort List
- LeetCode: Insertion Sort List 解题报告