leetcode-148. 排序链表

[复制链接]
发表于 2026-2-16 22:33:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
标题形貌
给你链表的头结点 head ,请将其按 升序 分列并返回 排序后的链表 。

示例 1:

  1. <strong>输入:</strong>head = [4,2,1,3]
  2. <strong>输出:</strong>[1,2,3,4]
复制代码
示例 2:

  1. <strong>输入:</strong>head = [-1,5,3,4,0]
  2. <strong>输出:</strong>[-1,0,3,4,5]
复制代码
示例 3:
  1. <strong>输入:</strong>head = []
  2. <strong>输出:</strong>[]
复制代码
思绪
使用快慢指针完成+归并两个有序链表完成归并排序
  1. # Definition for singly-linked list.
  2. class ListNode(object):
  3.     def __init__(self, val=0, next=None):
  4.         self.val = val
  5.         self.next = next
  6. class Solution(object):
  7.     def sortFunc(self, head, tail):
  8.         if not head:
  9.             return head
  10.         if head.next == tail:
  11.             head.next = None
  12.             return head
  13.         slow = fast = head
  14.         while fast != tail:
  15.             slow = slow.next
  16.             fast = fast.next
  17.             if fast != tail:
  18.                 fast = fast.next
  19.         mid = slow
  20.         return self.merge(self.sortFunc(head, mid), self.sortFunc(mid, tail))
  21.     def merge(self, head1, head2):
  22.         pre = ListNode(-1)
  23.         head, head1, head2 = pre, head1, head2
  24.         while head1 and head2:
  25.             if head1.val <= head2.val:
  26.                 head.next = head1
  27.                 head1 = head1.next
  28.             else:
  29.                 head.next = head2
  30.                 head2 = head2.next
  31.             head = head.next
  32.         if head1:
  33.             head.next = head1
  34.         if head2:
  35.             head.next = head2
  36.         return pre.next
  37.     def sortList(self, head):
  38.         """
  39.         :type head: ListNode
  40.         :rtype: ListNode
  41.         """
  42.         return self.sortFunc(head, None)
  43. if __name__ == '__main__':
  44.     s = Solution()
  45.     head = ListNode(1)
  46.     phead = head
  47.     data = [4, 3, 7, 5]
  48.     for i in data:
  49.         node = ListNode(i)
  50.         phead.next = node
  51.         phead = phead.next
  52.     head = s.sortList(head)
  53.     while head:
  54.         print(head.val),
  55.         head = head.next
复制代码
二刷:
  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution(object):
  7.     def sortFunc(self,head, tail):
  8.         if not head:
  9.             return None
  10.         if head.next == tail:
  11.             head.next = None
  12.             return head
  13.         slow = fast = head
  14.         while fast!=tail:
  15.             slow=slow.next
  16.             fast = fast.next
  17.             if fast!=tail:
  18.                 fast=fast.next
  19.         mid = slow
  20.         return self.merge(self.sortFunc(head,mid), self.sortFunc(mid,tail))
  21.     def merge(self, head1, head2):
  22.         pre = ListNode(-1)
  23.         head = pre
  24.         while head1 and head2:
  25.             if head1.val<head2.val:
  26.                 head.next = head1
  27.                 head1 = head1.next
  28.             else:
  29.                 head.next = head2
  30.                 head2 = head2.next
  31.             head = head.next
  32.         if head1:
  33.             head.next = head1
  34.         if head2:
  35.             head.next = head2
  36.         return pre.next
  37.     def sortList(self, head):
  38.         """
  39.         :type head: Optional[ListNode]
  40.         :rtype: Optional[ListNode]
  41.         """
  42.         return self.sortFunc(head,None)
复制代码
影象点:
快慢指针找中心节点+归并+头尾相连返回none
ps:主函数的入参是:head,None
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表