Delete Node in a Linked List LeetCode Solution

Last updated on October 10th, 2024 at 02:00 am

Here, We see Delete Node in a Linked List problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Topics

Linked List

Companies

Adobe, Apple, Microsoft

Level of Question

Medium

Delete Node in a Linked List LeetCode Solution

Delete Node in a Linked List LeetCode Solution

Problem Statement

There is a singly-linked list head and we want to delete a 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 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.
Delete Node in a Linked List example1
fig-1
Example 1: (fig-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.
Delete Node in a Linked List example2
fig-2
Example 2: (fig-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.

1. Delete Node in a Linked List Leetcode Solution C++

class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};

1.1 Explanation

  • Copy Value: Copy the value from the next node (node->next) into the current node (node).
  • Skip Node: Update the next pointer of the current node to skip the next node and point to the node after next (node->next->next).

1.2 Time Complexity

  • O(1).

1.3 Space Complexity

  • O(1).

2. Delete Node in a Linked List Leetcode Solution Java

class Solution {
    public void deleteNode(ListNode node) {
        if(node != null && node.next != null) {
                node.val = node.next.val;
                node.next = node.next.next;
            }
    }
}

2.1 Explanation

  • Check Node: Ensure that the node and its next node are not null to avoid null pointer exceptions.
  • Copy Value and Skip Node: Copy the value from the next node into the current node. Update the next pointer of the current node to skip the next node.

2.2 Time Complexity

  • O(1).

2.3 Space Complexity

  • O(1).

3. Delete Node in a Linked List Leetcode Solution JavaScript

var deleteNode = function(node) {
    node = Object.assign(node, node.next);
    node = null;
    return;
};

3.1 Explanation

  • Copy Node Properties: Use Object.assign to copy all properties (including val and next) from node.next to node.
  • Nullify Node (optional): Nullify the node reference to hint JavaScript’s garbage collector to release the node. This step is not strictly necessary for functionality but may help with memory management.

3.2 Time Complexity

  • O(1).

3.3 Space Complexity

  • O(1).

4. Delete Node in a Linked List Leetcode Solution Python

class Solution(object):
    def deleteNode(self, node):
        node.val=node.next.val
        node.next=node.next.next

4.1 Explanation

  • Copy Value: Copy the value from the next node (node.next) into the current node (node).
  • Skip Node: Update the next pointer of the current node to skip the next node and point to the node after next (node.next.next).

4.2 Time Complexity

  • O(1).

4.3 Space Complexity

  • O(1).
Scroll to Top