Sobes.tech
Junior — Senior

Removing a specified element from a singly linked list

livecode

Task condition

Given a singly linked list with a head node, and it is required to delete a specific node from it. The function will be given only the node itself, and access to the head of the list will not be provided. All values in the list are unique, and it is guaranteed that the node to be deleted is not the last element.

Deletion requirements:

  • After the operation, the value of the deleted node should not be present in the list.
  • The number of elements in the list should decrease by one.
  • The order of elements before the node should remain unchanged.
  • The order of elements after the node should also be preserved.

User testing: To verify, you need to pass the fully formed list (head) and the specific node to be deleted. The node is guaranteed not to be at the end of the list and is indeed present in it. The system will build the list, call your function with the specified node, and then check the resulting list.

Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: The second element with value 5 is deleted, and the list becomes 4 → 1 → 9.

Example 2: Input: head = [4,5,1,9], node = 1 Output: [4,5,9] Explanation: The third element with value 1 is deleted, and the list becomes 4 → 5 → 9.