|
| 1 | +<h2><a href="https://leetcode.com/problems/delete-node-in-a-linked-list">237. Delete Node in a Linked List</a></h2><h3>Medium</h3><hr><p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p> |
| 2 | + |
| 3 | +<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p> |
| 4 | + |
| 5 | +<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p> |
| 6 | + |
| 7 | +<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li>The value of the given node should not exist in the linked list.</li> |
| 11 | + <li>The number of nodes in the linked list should decrease by one.</li> |
| 12 | + <li>All the values before <code>node</code> should be in the same order.</li> |
| 13 | + <li>All the values after <code>node</code> should be in the same order.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p><strong>Custom testing:</strong></p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li> |
| 20 | + <li>We will build the linked list and pass the node to your function.</li> |
| 21 | + <li>The output will be the entire list after calling your function.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong class="example">Example 1:</strong></p> |
| 26 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" /> |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> head = [4,5,1,9], node = 5 |
| 29 | +<strong>Output:</strong> [4,1,9] |
| 30 | +<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" /> |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> head = [4,5,1,9], node = 1 |
| 37 | +<strong>Output:</strong> [4,5,9] |
| 38 | +<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li> |
| 46 | + <li><code>-1000 <= Node.val <= 1000</code></li> |
| 47 | + <li>The value of each node in the list is <strong>unique</strong>.</li> |
| 48 | + <li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li> |
| 49 | +</ul> |
0 commit comments