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
  • 相关阅读:
    linux
    ansible
    语法糖
    jupyter login
    hadoop patch
    ganglia
    unixbench安装使用
    linux使用FIO测试磁盘的iops
    cpu事实负载使用top命令
    phoronix-test-suite测试云服务器
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6512547.html
Copyright © 2011-2022 走看看