--- tags: leetcode --- # [237. Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) --- # My Solution ## The Key Idea for Solving This Coding Question Because the description guarantees that the given `node` is not the last node, we can copy the value of the next node (`node->next`) and then delete it. ## C++ Code ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode *node) { node->val = node->next->val; ListNode *x = node->next; node->next = x->next; delete x; } }; ``` ## Time Complexity $O(1)$ ## Space Complexity $O(1)$ # Miscellaneous <!-- # Test Cases ``` [4,5,1,9] 5 ``` ``` [4,5,1,9] 1 ``` ``` [1,2,3,4] 3 ``` ``` [0,1] 0 ``` ``` [-3,5,-99] -3 ``` -->