237. Delete Node in a Linked List
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The core challenge of this problem is to delete a node from a singly-linked list given only a reference to that specific node. We do not have access to the head of the list, which means we cannot traverse from the beginning to find the predecessor of the node to be deleted. The standard approach of updating the predecessor's next pointer to skip the target node is not feasible.
Solution
Delete Node in a Linked List in 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.
"""
# We are guaranteed that the given node is not the tail.
# The trick is to copy the value from the next node to the current node,
# and then effectively delete the next node.
# Get the next node
next_node = node.next
# Step 1: Copy the value of the next node to the current node.
node.val = next_node.val
# Step 2: Bypass the next node by pointing the current node's next pointer
# to the node after the next one.
node.next = next_node.next
# The original next_node is now orphaned and will be handled by Python's
# garbage collector.
Delete Node in a Linked List in 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) {
// The problem guarantees that the given node is not the last node in the list.
// This means node.next is always valid.
// To "delete" the current node without access to its predecessor,
// we can copy the value of the next node into the current node.
node.val = node.next.val;
// Then, we bypass the next node by linking the current node
// to the node after the next one.
node.next = node.next.next;
// The original next node is now unlinked and will be garbage collected.
}
}
Delete Node in a Linked List in C++
/**
* 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) {
// Since we cannot access the node before the one to be deleted,
// we can't just update the previous node's 'next' pointer.
// The trick is to copy the data from the next node to the current node,
// and then "delete" the next node.
// It's guaranteed that the node to be deleted is not the tail node,
// so 'node->next' will not be NULL.
ListNode* nextNode = node->next;
// Step 1: Copy the value from the next node to the current node.
node->val = nextNode->val;
// Step 2: Bypass the next node by updating the current node's 'next' pointer.
// This effectively removes the next node from the list.
node->next = nextNode->next;
// In a language without garbage collection like C++, the 'nextNode' is now
// unlinked but not freed. For this problem's constraints, we are not
// required to free the memory. The LeetCode judge only checks the
// resulting list structure. If memory management were required, we would:
// delete nextNode;
}
};
Delete Node in a Linked List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
Do not return anything, modify it in-place instead.
*/
function deleteNode(node: ListNode | null): void {
// Problem constraints guarantee that 'node' is a valid node in the list
// and is not the tail. This means 'node' and 'node.next' will not be null.
if (node === null || node.next === null) {
// This case is excluded by the problem's constraints.
return;
}
// The strategy is to copy the data from the next node into the current node,
// and then remove the next node from the list.
// Step 1: Copy the value from the successor node.
node.val = node.next.val;
// Step 2: Link the current node to the node after its successor,
// effectively bypassing/deleting the successor.
node.next = node.next.next;
// The original successor node is now unlinked and will be garbage collected.
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.