zoukankan      html  css  js  c++  java
  • [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                     
    View Code
  • 相关阅读:
    zipfile和tarfile的简单使用方法
    RabbitMQ安装
    postman接口自动化
    linux命令
    redis安装部署和使用
    nmon使用
    jdk自带监控工具配置使用
    修改本机mac
    hashlib模块,md5加密
    tomcat部署
  • 原文地址:https://www.cnblogs.com/chruny/p/5478018.html
Copyright © 2011-2022 走看看