看板Marginalman
: 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