237. Delete Node in a Linked List

237. Delete Node in a Linked List

Problem Solving - Day 13

ยท

4 min read

Hello, reader ๐Ÿ‘‹๐Ÿฝ ! Welcome to day 13 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.

Today, I will be picking up LeetCode's daily challenge problem: 237. Delete Node in a Linked List.


๐Ÿค” Problem Statement

  • Given the node in a singly linked list, we need to delete it. The head of the list is not given.
  • 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.
  • Deleting the node does not mean removing from the memory. Rather it means:

    • 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.
  • E.g.

    • head = [5,4,1,2], given node: 4 => [5,1,2].
    • head = [4,5,1,9], given node = 1 => [4,5,9].

๐Ÿ’ฌ Initial Thoughts

  • If you are given a linked list and the node to be deleted, you'd usually traverse until you reach the said node, and update the next pointer of it's previous node.
  • But in this problem, we are not given the head of the list. And without the head, it's not possible to traverse until the node to be deleted.
  • Then how do we update the pointers so that the node to be deleted is no longer a part of the list?
  • The first clue for this is that all the values are unique and all we have to do is to ensure that the number of nodes in the list decreases by 1.
  • So why not update the value of the node to be deleted itself?
  • How? Well we can edit the value of the current node with the value of the next node.
  • But this still leaves the same number of nodes in the list and the same value repeated twice.
  • So far we've only achieved this: delete_given_node1.png

  • Also note that we don't have to worry about updating the order of the nodes before the given node to be deleted as we anyway don't have access to those.

  • All we have to focus is keeping the values to the right of the node in the same order, simultaneously decreasing the number of nodes.
  • One of the easiest way to decrease the number of nodes is to update the pointers.
  • How can we do that? We can simply point the node to be deleted (whose value we just updated) to the next pointer of the immediate node.
  • This will reduce the number of nodes as while maintaining the order of nodes after the node to be deleted.
  • Our final state is like this: delete_node2.png

  • These two steps does it! Let's look into the code now.

๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป Solution

  • Below is the code for the above approach

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public void deleteNode(ListNode node) {
            // no node to be deleted
            if(node == null) {
                return;
            }
    
            ListNode curr = node;
            ListNode nextNode = curr.next;
            curr.val = nextNode.val;
            curr.next = nextNode.next;
            nextNode.next = null;
        }
    }
    
    Time Complexity: O(1)
        - Since we aren't traversing all the nodes and only accessing the next node of the node to be deleted.
    Space Complexity: O(1)
        - Since we use memory to only hold the node.
    

You can find the code for this problem on GitHub repo: 237. Delete Node in a Linked List.


Conclusion

That's a wrap for today's problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!

Thank you for reading!