Delete Node In A Linked List

To solve this coding challenge, you need to manipulate a singly-linked list such that a given node (excluding the last node) is effectively "deleted" from the list. A clever trick here involves copying the value from the next node into the node to be "deleted," then unlinking the next node.

Explanation

In a singly linked list, each node contains two parts: a value and a reference (or pointer) to the next node in the sequence. The challenge specifies that you will be given access only to the node to be deleted and not the head of the list. This is a crucial detail that influences the approach.

Detailed Steps in Pseudocode:

Step-by-Step Explanation
  1. Understand Node Structure: Each node in a singly-linked list typically contains a value and a reference to the next node. Let's call the node to be deleted
    node
    .
  2. Copy Next Node’s Value: Take the value from the node that comes immediately after
    node
    (i.e.,
    node.next
    ) and copy it into
    node
    . This effectively overwrites the current node's value with the value of the next node.
  3. Skip the Next Node: Adjust the
    next
    pointer of
    node
    to skip over the immediate next node. This can be done by setting
    node.next
    to
    node.next.next
    . This step essentially removes the next node from the list by bypassing it in the sequence.
  4. Completion: After these operations, the
    node
    will contain the value that was in
    node.next
    , and the list will effectively skip over the original
    node.next
    .
  5. Pseudocode with Comments:
                                                
    # Define the function which takes the node to be deleted
    function deleteNode(node):
    # Step 1: Copy the value from the next node into the current node
    node.value = node.next.value
    
    # Step 2: Bypass the next node by pointing this node's next to next node's next
    node.next = node.next.next
    
                                            
    Detailed Steps in Pseudocode
  6. Step 1: Copy the Value
    • Access the value of the node that comes immediately after the current node (i.e.,
      node.next.value
      ).
    • Overwrite the current node's value with this value. This makes the current node act as if it is the next node.
  7. Step 2: Adjust the Next Pointer
    • Change the
      next
      pointer of the current node to point to the node after the next node (i.e.,
      node.next.next
      ).
    • This step effectively removes the "next" node from the linked list sequence because no nodes will be directly pointing to it anymore.
    Example Walkthrough
    Let's illustrate these steps using an example from the problem description:
    Example 1:
  8. Input: head = [4,5,1,9], node = 5
    • The node to be deleted has the value 5.
    Before deletion:
  9. List:
    4 -> 5 -> 1 -> 9
  10. Step-by-Step Execution:
  11. Copy the value from the next node:
    • node.value
      is
      5
      , and
      node.next.value
      is
      1
      .
    • After copying,
      node.value
      becomes
      1
      .
    • List state:
      4 -> 1 -> 1 -> 9
  12. Adjust the pointer:
    • node.next
      is currently pointing to the node with value
      1
      .
    • Change
      node.next
      to point to
      node.next.next
      , which is the node with value
      9
      .
    • List state after deletion:
      4 -> 1 -> 9
By following these detailed steps closely, you can ensure that the node is effectively deleted from the list without requiring access to the head of the list. The pseudocode provided gives a clear and concise way to achieve this in a coding implementation.