[leetcode_237]Delete Node in a Linked List

Delete a specified node in a singly linked list. When I first read the problem, I thought at least both the head node and the target node should be given. But after guessing, I realized the problem likely requires copying the values of subsequent nodes forward to overwrite, rather than performing a true linked list deletion.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode * next = node->next;
        ListNode * before = NULL;
        while(next != NULL)
        {
            node->val = next->val;
            before = node;
            node = next;
            next = next->next;
        }
        before->next = NULL;
    }
};