[LeetCode]题解(python):147-Insertion Sort List
题目来源:
https://leetcode.com/problems/insertion-sort-list/
题意分析:
用插入排序排序一个链表。
题目思路:
这题没什么好说的,直接用插入排序就行。
代码(python):
1 # Definition for singly-linked list. 2 # class ListNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution(object): 8 def insertionSortList(self, head): 9 """ 10 :type head: ListNode 11 :rtype: ListNode 12 """ 13 if head == None: 14 return head 15 tmp = ListNode(0) 16 tmp.next,p = head,head 17 while p.next: 18 if p.next.val < p.val: 19 tmp1 = tmp 20 while tmp1.next.val < p.next.val: 21 tmp1 = tmp1.next 22 t = p.next 23 p.next = t.next 24 t.next = tmp1.next 25 tmp1.next = t 26 else: 27 p = p.next 28 return tmp.next 29 30