148. 排序链表 - 力扣(LeetCode)

暴力算法

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 10:51 
# "Stay hungry,stay foolish."

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

class Solution(object):
    def sortList(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        # Step 1: 提取所有值
        cur = head
        values = []
        while cur:
            values.append(cur.val)
            cur = cur.next

        # Step 2: 排序(注意:题目是排序链表,不是反转!)
        values.sort()  # 升序排序

        # Step 3: 重建链表
        dummy = ListNode(0)  # 哑节点,方便操作
        current = dummy
        for val in values:
            current.next = ListNode(val)
            current = current.next

        return dummy.next

归并算法

大佬图示:

代码:

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 10:51 
# "Stay hungry,stay foolish."

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

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

        left = head
        right = self.getMid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return self.merge(left, right)

    def getMid(self, head):
        fast = head.next
        slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow

    def merge(self, list1, list2):
        tail = dummy = ListNode()
        while list1 and list2:
            if list1.val < list2.val:
                tail.next = list1
                list1 = list1.next
            else:
                tail.next = list2
                list2 = list2.next
            tail = tail.next

        # 其中一个链为None,另一个可能不是None
        if list1:
            tail.next = list1

        if list2:
            tail.next = list2

        return dummy.next

结果

解题步骤:

相关推荐
凌冰_6 小时前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
Pniubi7 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
无忧.芙桃11 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
郝学胜-神的一滴13 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
文丑颜不良啊13 小时前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode3421~3440题目解析
java·leetcode
console.log('npc')18 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
LK_072 天前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
Pniubi2 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展