Delete Node in a Linked List

medium linked list

Problem

You are given a node (not the tail) of a singly linked list. Delete that node from the list. You don't have access to the head — only the node you must delete.

Inputhead = [4,5,1,9], node = 5
Output[4,1,9]
Copy 1 into the 5-node, then point past the original 1 node.

def delete_node(node):
    node.val = node.next.val
    node.next = node.next.next
function deleteNode(node) {
  node.val = node.next.val;
  node.next = node.next.next;
}
class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}
void deleteNode(ListNode* node) {
    node->val = node->next->val;
    node->next = node->next->next;
}
Time: O(1) Space: O(1)