码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode sort]147. Insertion Sort List

时间:2017-03-07 00:30:42      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:turn   link   数组   利用   位置   return   list   插入排序   ons   

Sort a linked list using insertion sort.

利用插入排序对一个链表进行排序

思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可以从要插入的位置开始从后往前找合适的位置

 1 class Solution(object):
 2     def insertionSortList(self, head):
 3         dummy = ListNode(-1)
 4         dummy.next,cur= head,head
 5         while cur and cur.next:
 6             if cur.val > cur.next.val:
 7                 head = dummy
 8                 while head.next.val < cur.next.val:
 9                     head = head.next
10                 head.next,cur.next.next,cur.next = cur.next,head.next,cur.next.next
11             else:
12                 cur = cur.next
13         return dummy.next

 

[leetcode sort]147. Insertion Sort List

标签:turn   link   数组   利用   位置   return   list   插入排序   ons   

原文地址:http://www.cnblogs.com/fcyworld/p/6512547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!