[leetcode]Insertion Sort List @ Python
2014-04-29 21:13
525 查看
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/
题意:对链表进行插入排序。
解题思路:首先来对插入排序有一个直观的认识,来自维基百科。
代码循环部分图示:
代码:
题意:对链表进行插入排序。
解题思路:首先来对插入排序有一个直观的认识,来自维基百科。
代码循环部分图示:
代码:
class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = ListNode(0) #为链表加一个头节点 dummy.next = head curr = head while curr.next: if curr.next.val < curr.val: #如果链表是升序的,那么curr指针一直往后移动 pre = dummy #直到一个节点的值小于前面节点的值 while pre.next.val < curr.next.val: #然后寻找插入的位置 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 with Python】 Insertion Sort List
- [leetcode] Insertion Sort List(python)
- LeetCode-Insertion Sort List (Python)
- [leetcode] Insertion Sort List(python)
- 【LEETCODE】147- Insertion Sort List [Python]
- [Leetcode][python]Insertion Sort List
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- Insertion Sort List Leetcode Python
- leetcode 【 Insertion Sort List 】 python 实现
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- Insertion Sort List (LeetCode)
- leetCode #147 Insertion Sort List
- [Leetcode][JAVA] Insertion Sort List
- LeetCode problem 5: Insertion Sort List
- Insertion Sort List —— Leetcode
- 【LeetCode】Insertion Sort List
- 【LeetCode】-Insertion Sort List
- leetcode 刷题之路 27 Insertion Sort List
- LeetCode | Insertion Sort List