Re: [閒聊] 每日leetcode

作者: argorok (s.green)   2024-05-06 10:08:15
※ 引述《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
作者: digua (地瓜)   2024-05-06 10:22:00
大師
作者: DJYOSHITAKA (Evans)   2024-05-06 11:15:00
大濕

Links booklink

Contact Us: admin [ a t ] ucptt.com