热题系列章节20

25. K 个一组翻转链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from queue import PriorityQueue
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        nums = []
        
        for l in lists:
            while l:
                nums.append(l.val)
                l = l.next

        nums.sort()

        cur = head = ListNode(0)
        for i in nums:
            cur.next = ListNode(val=i)
            cur= cur.next

        return head.next

5. 最长回文子串

33. 搜索旋转排序数组

200. 岛屿数量

92. 反转链表 II

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        head=ListNode(0)
        # 创建一个指针用于遍历合并链表
        current=head
        # 当两个链表同时都有节点时
        while list1 and list2:
            # 如果链表l1节点的值>=l2节点的值,将l2当前节点赋予current的下一个节点
            # 将l2指向其下一个节点
            if list1.val>=list2.val:
                current.next=list2
                list2=list2.next
            else:
                current.next=list1
                list1=list1.next
            current=current.next
        # 将剩余部分连接到合并链表的末尾
        current.next= list1 if list1 else list2

        return head.next

23. 合并K个排序链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from queue import PriorityQueue
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        nums = []
        
        for l in lists:
            while l:
                nums.append(l.val)
                l = l.next

        nums.sort()

        cur = head = ListNode(0)
        for i in nums:
            cur.next = ListNode(val=i)
            cur= cur.next

        return head.next

143. 重排链表

class Solution(object):
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: None Do not return anything, modify head in-place instead.
        """
        if not head or not head.next: return head
        # 1 2 3 4 5
        fast = head
        pre_mid = head
        # 找到中点, 偶数个找到时上界那个
        while fast.next and fast.next.next:
            pre_mid = pre_mid.next
            fast = fast.next.next
        # 翻转中点之后的链表,采用是pre, cur双指针方法
        pre = None
        cur = pre_mid.next
        # 1 2 5 4 3
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        # 翻转链表和前面链表拼接
        pre_mid.next = pre
        # 1 5 2 4 3
        # 链表头
        p1 = head
        # 翻转头
        p2 = pre_mid.next
        #print(p1.val, p2.val)
        while p1 != pre_mid:
            # 建议大家这部分画图, 很容易理解
            pre_mid.next = p2.next
            p2.next = p1.next
            p1.next = p2
            p1 = p2.next
            p2 = pre_mid.next

124. 二叉树中的最大路径和

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        self.ans = float("-inf")
        def maxGain(r):
            if not r:
                return 0 
            left = max(maxGain(r.left), 0) # 注意:这里的需要保存大于0 的根
            right = max(maxGain(r.right), 0)
            self.ans = max(self.ans, r.val+left+right) # 不然遇到[2,-1]的情况会出错
            return r.val+max(left, right)
        maxGain(root)
        return self.ans 

补充题23. 检测循环依赖

468. 验证IP地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值