※ 引述《Rushia (早瀬ユウカの体操服 )》之铭言:
: 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