Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (早瀬ユウカの体操服 )時間1年前 (2024/05/06 09:55), 編輯推噓2(201)
留言3則, 3人參與, 1年前最新討論串188/1548 (看更多)
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
文章代碼(AID): #1cE3YFpy (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cE3YFpy (Marginalman)