Re: [閒聊] 每日leetcode

看板 Marginalman
作者 argorok (死肥肥社管)
時間 2024-05-06 10:08:15
留言 1則留言 (0推 0噓 1→)

: https://leetcode.com/problems/remove-nodes-from-linked-list/description/ : 2487. Remove Nodes From Linked List : 給你一個鏈結串列,移除串列中所有右邊存在比他大的數字的的節點。 : https://assets.leetcode.com/uploads/2022/10/02/drawio.png
思路: 直接DFS+抄昨天那題答案 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: if head.next: self.removeNodes(head.next) else: return head if head.val < head.next.val: head.val = head.next.val head.next = head.next.next return head --
※ 批踢踢實業坊(ptt.cc), 來自: 36.228.97.180 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1714961297.A.BA0.html

digua: 大師 05/06 10:22

您可能感興趣