精華區beta Marginalman 關於我們 聯絡資訊
237. Delete Node in a Linked List 給予一個node,從該鏈結串列刪除這個node的值。 Example: https://assets.leetcode.com/uploads/2020/09/01/node1.jpg
思路: 1.因為題目說提供的node一定不是最後一個,所以直接改他的值當作新的下個節點 2.https://i.imgur.com/Zq0iBJK.jpeg JavaCode: class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } 其實我看不懂這題再考三小這題為啥是Medium? 題目要我們刪除節點還不給我們頭節點 我倒讚了 這個月題目是不是不太行 -- https://i.imgur.com/7bZXdBG.jpg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.89.219 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1665623565.A.BA6.html
Ericz7000: 大師 10/13 09:29
surimodo: 你這解法太粗暴惹 10/13 09:30
surimodo: 而且也不是真正意義上的刪除ㄅ 只是指不到? 10/13 09:31
surimodo: 理想做法應該是pop出來? 10/13 09:32
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean: * The value of the given node should not exist in the linked list. * The number of nodes in the linked list should decrease by one. * All the values before node should be in the same order. * All the values after node should be in the same order. ※ 編輯: Rushia (1.160.89.219 臺灣), 10/13/2022 09:36:07
pandix: 應該是因為平常linked list的題目都會要求不能改value 10/13 09:47
pandix: 這題想提醒你一下而已 10/13 09:47
SecondRun: 大師 10/13 10:26
hw102050: 這樣原本的node(1),不就沒人指了? 10/13 10:45
Rushia: 在鏈結串列裡面 沒人指就和刪除差不多了 對ㄚ 10/13 11:36
GTR12534: 會佔用記憶體吧 嗎 10/13 18:53