zoukankan      html  css  js  c++  java
  • 链表_leetcode147

    # Definition for singly-linked list.
    class ListNode(object):
    def __init__(self, x):
    self.val = x
    self.next = None

    class Solution(object):
    def insertionSortList(self, head):
    """
    :type head: ListNode
    :rtype: ListNode
    """

    if not head or not head.next:
    return head


    dummyHead = ListNode(0)
    dummyHead.next = head
    pre = dummyHead

    cur = head.next



    p = head
    p.next = None

    while cur:

    while p:

    if cur.val < p.val:
    nextCur = cur.next

    cur.next = p
    pre.next = cur
    cur = nextCur

    pre = dummyHead
    p = pre.next

    break
    else:
    pre = p
    p = p.next


    if not p:

    nextCur = cur.next
    pre.next = cur
    cur.next = None

    cur = nextCur
    pre = dummyHead
    p = pre.next




    return dummyHead.next


    def creatList(self,l):
    dummyHead = ListNode(0)

    pre = dummyHead

    for i in l:
    pre.next = ListNode(i)
    pre = pre.next

    return dummyHead.next


    def printList(self,head):
    cur = head

    while cur:
    print cur.val
    cur = cur.next




    l1 = [4,2,1,3]

    s = Solution()

    head = s.creatList(l1)

    s.printList(head)

    head = s.insertionSortList(head)

    s.printList(head)
  • 相关阅读:
    剩下的树
    守形数
    小白鼠排队(map容器插入数据的四种方法)
    字母统计
    与7无关的数
    ZOJ
    基于js的CURD插件
    API验证插件
    Django之权限管理插件
    Django之信号和序列化
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557210.html
Copyright © 2011-2022 走看看