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:
node
should be in the same order.node
should be in the same order.Custom testing:
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.Example 1:
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:
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:
[2, 1000]
.-1000 <= Node.val <= 1000
node
to be deleted is in the list and is not a tail node.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for removing a node in a linked list involves searching for the node we want to get rid of by looking at each node one by one from the beginning. Once found, we rearrange connections to bypass the unwanted node. The brute force method would not generally be used for this problem because we are given access to the node to delete.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, value=0, next_node=None):
self.value = value
self.next_node = next_node
def delete_node_brute_force(head, node_to_delete):
if head is None:
return None
# If the node to delete is the head, update the head
if head == node_to_delete:
head = head.next_node
return head
current_node = head
# Iterate until the end of the list.
while current_node is not None:
# Find the node before the node to delete.
if current_node.next_node == node_to_delete:
# Skip the node to be deleted.
current_node.next_node = node_to_delete.next_node
return head
current_node = current_node.next_node
return head
Since we are given the node to delete and not the head of the list, we cannot traverse to find the node before the one we want to delete. Instead, we'll overwrite the node to be deleted with the contents of the next node, then bypass the next node, effectively deleting it.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, value=0, next_node=None):
self.value = value
self.next_node = next_node
def delete_node(node_to_delete):
# Copy the value from the next node to the node to be deleted.
node_to_delete.value = node_to_delete.next_node.value
# Update the next pointer to skip the actual next node.
node_to_delete.next_node = node_to_delete.next_node.next_node
Case | How to Handle |
---|---|
Node is null | While the problem states the node to delete is given, a null node should raise an IllegalArgumentException. |
Node is the tail of the list | Copy the value from node.next to node and then set node.next to null. |
Node to delete is the only node in the list | This scenario is technically impossible based on the problem description, but if encountered, one approach could be to set the node's value to null; however, a proper solution would be to raise an exception because you can't delete the only node if you don't have the head pointer. |
List contains only two nodes and node is the first node | Copy the value from node.next into node and then set node.next to null, effectively removing the second node. |
Large list: Check for potential memory issues (though unlikely with this algorithm) | The algorithm operates in-place and has O(1) space complexity, so memory is unlikely to be an issue even for large lists. |
Values stored in the list are of different types (e.g., int, string, object) | The algorithm works regardless of the type of data stored in the nodes as it only swaps the values and pointers and doesn't perform calculations. |
Node is not actually in the linked list | Since we don't have the head, we can't verify this; either this is considered invalid input, or no operation is performed if node.next is null as described in another case. |
List has cycles which can cause infinite loop. | Singly linked lists should not contain cycles, but if one exists it would cause an infinite loop; checking for cycles is generally beyond the scope of this problem but a time out would prevent a true infinite loop. |