您的位置:首页 > 编程语言 > Python开发

[LeetCode]题解(python):147-Insertion Sort List

2016-05-10 15:31 639 查看
[b]题目来源:[/b]

  https://leetcode.com/problems/insertion-sort-list/

[b]题意分析:[/b]

  用插入排序排序一个链表。

[b]题目思路:[/b]

  这题没什么好说的,直接用插入排序就行。

[b]代码(python):[/b]

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return head
tmp = ListNode(0)
tmp.next,p = head,head
while p.next:
if p.next.val < p.val:
tmp1 = tmp
while tmp1.next.val < p.next.val:
tmp1 = tmp1.next
t = p.next
p.next = t.next
t.next = tmp1.next
tmp1.next = t
else:
p = p.next
return tmp.next


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: