zoukankan      html  css  js  c++  java
  • 链表_leetcode148-链表归并排序

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


    class Solution:
    def sortList(self, head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if not head or not head.next:
    return head

    mid = self.getMid(head)

    l = head
    r = mid.next
    mid.next =None
    return self.merge(self.sortList(l),self.sortList(r))

    def getMid(self,head):
    if not head :
    return head
    fast = slow = head

    while fast.next and fast.next.next:
    slow = fast.next
    fast = fast.next.next

    return slow


    def merge(self,p,q):
    dummyHead = ListNode(0)
    pre = dummyHead
    while p and q:
    if p.val < q.val:
    pre.next = p
    pre = pre.next
    p = p .next
    else:
    pre.next = q
    pre = pre.next
    q = q.next

    if p:
    pre.next = p
    else:
    pre.next = q

    return dummyHead.next
  • 相关阅读:
    hibernate的核心配置
    hibernate的映射配置
    数据库的维护
    索引
    数据库规范化设计
    数据控制DCL
    触发器
    SQL存储过程简介
    Transact-SQL简介
    sysdatabaes表与sysobjects表
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557221.html
Copyright © 2011-2022 走看看