0
点赞
收藏
分享

微信扫一扫

题目地址(148. 排序链表)

IT程序员 2022-01-05 阅读 65

题目地址(148. 排序链表)

https://leetcode-cn.com/problems/sort-list/

题目描述

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

 

示例 1:

输入:head = [4,2,1,3]
输出:[1,2,3,4]


示例 2:

输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]


示例 3:

输入:head = []
输出:[]


 

提示:

链表中节点的数目在范围 [0, 5 * 104] 内
-105 <= Node.val <= 105

 

进阶:你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

前置知识

公司

  • 暂无

思路

关键点

代码

解法一

  • 语言支持:Python3

Python3 Code:


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # 转换为线性表
        if not head or not head.next:
            return head
        mid = self.midNode(head)
        temp = mid.next 
        mid.next = None 
        head1 = self.sortList(head)
        head2 = self.sortList(temp)
        dummpy_head = ListNode()
        dummpy_head_copy = dummpy_head
        while head1 and head2:
            if head1.val < head2.val:
                dummpy_head.next = head1
                head1 = head1.next
            else:
                dummpy_head.next = head2
                head2 = head2.next
            dummpy_head = dummpy_head.next 
        if head1:
            dummpy_head.next = head1
        else:
            dummpy_head.next = head2
        return dummpy_head_copy.next
    def midNode(self, head:ListNode)->ListNode:
        slow = fast = head 
        while fast.next and fast.next.next:
            slow = slow.next 
            fast = fast.next.next
        return slow 
    

复杂度分析

令 n 为数组长度。

  • 时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度: O ( l o g n ) O(logn) O(logn)

解法二

  • 语言支持:Python3

Python3 Code:


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def merge(head1: ListNode, head2: ListNode)->ListNode:
            dummyHead = ListNode(0)
            temp, temp1, temp2 = dummyHead, head1, head2
            while temp1 and temp2:
                if temp1.val < temp2.val:
                    temp.next = temp1
                    temp1 = temp1.next
                else:
                    temp.next = temp2
                    temp2 = temp2.next
                temp = temp.next 
            temp.next = temp1 if temp1 else temp2
            return dummyHead.next 
        
        if not head:
            return head 
        length = 0 
        node = head 
        while node: node,length = node.next, length+1

        dummyHead = ListNode(0,head)
        subLength = 1
        while subLength < length:
            prev, curr = dummyHead, dummyHead.next
            while curr:
                head1 = curr
                for i in range(1, subLength):
                    if curr.next:
                        curr = curr.next
                    else:
                        break
                head2 = curr.next 
                curr.next = None 
                curr = head2
                for i in range(1, subLength):
                    if curr and curr.next:
                        curr = curr.next
                    else:
                        break
                succ = None 
                if curr:
                    succ = curr.next 
                    curr.next = None 
                merged = merge(head1,head2)
                prev.next = merged
                while prev.next:
                    prev = prev.next
                curr = succ
            subLength <<= 1 
        return dummyHead.next

复杂度分析

令 n 为数组长度。

  • 时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度: O ( 1 ) O(1) O(1)
举报

相关推荐

0 条评论