Leetcode 237: Delete Node in a Linked List

grid47
grid47
Exploring patterns and algorithms
Oct 14, 2024 4 min read

A linked list where a node gently fades away, leaving a cleaner structure behind.
Solution to LeetCode 237: Delete Node in a Linked List Problem

You are given a node in a singly linked list, and you are asked to delete this node from the list. The node is guaranteed to be not the last node in the list. After the deletion, the values before the node should remain in the same order, and the values after the node should also remain in the same order.
Problem
Approach
Steps
Complexity
Input: You are provided with the head of the linked list and a specific node to delete. The node will not be the last node in the list.
Example: Input: head = [1,2,3,4,5], node = 3
Constraints:
• The linked list will have at least two nodes.
• The value of each node is unique.
• The node to be deleted is guaranteed to not be the tail node.
Output: After deleting the given node, return the modified linked list with the node removed.
Example: Output: [1,2,4,5]
Constraints:
Goal: To delete the given node, we will copy the value of the next node into the current node and then delete the next node.
Steps:
• Copy the value of the next node into the current node.
• Point the current node's next to the node after the next node.
• Delete the next node.
Goal: The list will not be empty, and the given node will not be the last node in the list.
Steps:
Assumptions:
• The list is not empty.
• The node to be deleted is not the last node.
Input: Input: head = [1,2,3,4,5], node = 3
Explanation: The node to be deleted is 3. After deleting 3, the list becomes [1,2,4,5].

Input: Input: head = [10,20,30,40], node = 20
Explanation: The node to be deleted is 20. After deleting 20, the list becomes [10,30,40].

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus