# Delete Node in a Linked List
**Difficulty:** MEDIUM
[External](https://leetcode.com/problems/delete-node-in-a-linked-list)
Canonical: https://scaleengineer.com/dsa/problems/delete-node-in-a-linked-list
**Data structures:** Linked List
**Companies:** [Nvidia](https://scaleengineer.com/companies/nvidia), [Oracle](https://scaleengineer.com/companies/oracle), [PayPal](https://scaleengineer.com/companies/paypal)
---
## Problem
There is a singly-linked list `head` and we want to delete a node `node` in it.

You are given the node to be deleted `node`. You will **not be given access** to the first node of `head`.

All the values of the linked list are **unique**, and it is guaranteed that the given node `node` is not the last node in the linked list.

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

* The value of the given node should not exist in the linked list.
* The number of nodes in the linked list should decrease by one.
* All the values before `node` should be in the same order.
* All the values after `node` should be in the same order.

**Custom testing:**

* For the input, you should provide the entire linked list `head` and the node to be given `node`. `node` should not be the last node of the list and should be an actual node in the list.
* We will build the linked list and pass the node to your function.
* The output will be the entire list after calling your function.

**Example 1:**

![](https://assets.glich.co/dsa/delete-node-in-a-linked-list/image0.jpg) 

**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:**

![](https://assets.glich.co/dsa/delete-node-in-a-linked-list/image1.jpg) 

**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.

**Constraints:**

* The number of the nodes in the given list is in the range `[2, 1000]`.
* `-1000 <= Node.val <= 1000`
* The value of each node in the list is **unique**.
* The `node` to be deleted is **in the list** and is **not a tail** node.

# Approaches
## Copy Next Node Value and Delete Next Node
Since we don't have access to the head of the linked list and only have access to the node to be deleted, we can copy the value of the next node to the current node and delete the next node.
**Time:** O(1) - We only perform two operations regardless of the list size · **Space:** O(1) - We don't use any extra space
**Pros:** Very simple and elegant solution; Constant time complexity; Constant space complexity; No need to traverse the list
**Cons:** Only works when the node to be deleted is not the last node; Modifies the values of nodes instead of actually deleting the node; May not work if the nodes are immutable
### Explanation
The key insight to solve this problem is that since we don't have access to the previous node, we can't directly delete the given node. However, we can copy the value of the next node to the current node and then delete the next node. This effectively makes it appear as if we deleted the current node.

Here's how it works:
1. Copy the value of the next node to the current node
2. Update the next pointer of the current node to skip the next node

Here's the implementation:

```java
class Solution {
    public void deleteNode(ListNode node) {
        // Copy the value of the next node to current node
        node.val = node.next.val;
        
        // Update the next pointer to skip the next node
        node.next = node.next.next;
    }
}
```

For example, if we have a linked list 4->5->1->9 and we want to delete node with value 5:
1. Copy value 1 to node 5 (becomes 4->1->1->9)
2. Update next pointer of first 1 to point to 9 (becomes 4->1->9)
### Algorithm
1. node.val = node.next.val
2. node.next = node.next.next

# Solutions
### CSharp

```csharp
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public void DeleteNode ( ListNode node ) { node . val = node . next . val ; node . next = node . next . next ; } }
```

### Java

```java
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void deleteNode ( ListNode node ) { node . val = node . next . val ; node . next = node . next . next ; } }
```

### JavaScript

```javascript
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} node * @return {void} Do not return anything, modify node in-place instead. */ var deleteNode =
  function (node) {
    node.val = node.next.val;
    node.next = node.next.next;
  };

```

### Python

```python
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution : def deleteNode ( self , node ): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node . val = node . next . val node . next = node . next . next
```

### CPP

```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 ; node -> next = node -> next -> next ; } };
```
