Re: [閒聊] 每日leetcode已回收
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

思路:
1.題目其實是在求一個最長遞減串列,我們將原串列做反轉之後把遞增的串起來,再把
串起來的串列也反轉即可。
py code:
----------------------------------------------
class Solution:
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
def reverse(node: Optional[ListNode]):
curr, prev = node, None
while curr:
tmp = curr.next
curr.next = prev
prev = curr
curr = tmp
return prev
head = reverse(head)
dummy = ListNode(-1)
curr = dummy
while head:
if head.val >= curr.val:
curr.next = head
curr = curr.next
head = head.next
curr.next = None
return reverse(dummy.next)
----------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.138.52.131 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1714960527.A.CFC.html
→
05/06 10:01,
1年前
, 1F
05/06 10:01, 1F
推
05/06 10:20,
1年前
, 2F
05/06 10:20, 2F
推
05/06 10:22,
1年前
, 3F
05/06 10:22, 3F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 188 之 1548 篇):