Pointer to head of linked list missing from expected solution for "Delete Node in a Singly Linked List"

  • 5

    For "Delete Node in a Linked List", the soln is expecting only the node to be deleted and not the original list. Without the head of the list how are we expected to traverse the singly linked list to get to the previous node to delete in place? Please see https://leetcode.com/problems/delete-node-in-a-linked-list/

  • 14

    This problem is quite famous (you can find it in several programming interviews books) and indeed you do not need a pointer to the head of the list if the problem statement is more clearly stated:

    The objective is to edit the values in the nodes so that the resulting list is equivalent to having deleted the given node in the list. I.e. you cannot remove the node as a Node instance from the list but you can edit its value to the value of the next node and the value of the next node to the next to the next node and so on.

    I.e. if you have the list:

    1 -> 2 -> 3 -> 4 -> 5 -> null

    And you are given a reference to node 3, you can edit node 3 to contain the value 4 and node 4 to contain the value 5 and point to null resulting in the following list:

    1 -> 2 -> 4 -> 5 -> null

    If you were given a reference to node 5, you would not be able to do so. For this reason the problem specifies that you can be given any node but the tail of the list.

  • 0

    Thanks a lot for the answer!

  • 0

    You are welcome :-)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.