2020年4月20日 星期一

[LeetCode] 237. Delete Node in a Linked List* 解題思路 (Easy)



這題需要刪除 LinkedList 某一個節點。




LeetCode 題目連結

 

https://leetcode.com/problems/delete-node-in-a-linked-list/

 

題目



Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Given linked list -- head = [4,5,1,9], which looks like following:

Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
Example 2:
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.

Accept 作法

這題就只是把節點的 next 替代成 next 的 next


Runtime: 0 ms
Memory: 39.1 MB

Java 程式碼

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
         node.next = node.next.next;
    }
}



 

更多 LeetCode 相關資源

 

複習程式面試書籍


除了 LeetCode 練習外,我也入手了這本,題庫來自真正的面試,並非摘自教科書。它們反映出頂尖公司真正會出的題目,你可以藉此做好充分準備
需要的話可以看看,寫得很仔細。





書名:提升程式設計師的面試力:189道面試題目與解答




相關 LeetCode文章一律會放在 程式解題 標籤分類,歡迎持續追蹤。


沒有留言:

張貼留言