# \#237 Delete Node in a Linked List ## *在只給定node的情況下, 刪除linked list的這個node* ## Log - build 20210518 by syhuang ## 改進 - 不必跑迴圈, 直接把node.next.val蓋過來, 然後node.next = node.next.next即可(等同刪除下一個節點) ```javascript= var deleteNode = function(node) { node.val = node.next.val; node.next = node.next.next; }; ``` ## 初見 - 把下一個node的val跑迴圈不斷往回覆蓋, 直到最後一個node, 再把前一個node.next清除, 就達到"清除node"的效果 ```javascript= var deleteNode = function(node) { var t = node; var preNode; while(t.next){ t.val = t.next.val; preNode = t; t = t.next; } preNode.next = null; return; }; ``` ## 備註 - 因為題目指明了不給head的前題下做"刪除"動作, 會乍有無解的感覺, 但實際上用點小trick能解 ## 參考 ###### tags: `leetcode`, `leetcode-easy`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up