zoukankan      html  css  js  c++  java
  • [leetcode sort]147. Insertion Sort List

    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
  • 相关阅读:
    文件上传案例_Socket_测试
    Linux的小整理(入门)
    full stack 第一天
    基础考题 试题
    shell语法
    网络管理
    图像类
    定时储存
    网络管理
    磁盘管理
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6512547.html
Copyright © 2011-2022 走看看