Delete Node in a Linked List
Let's learn about linked lists by helping you master the art of deleting a node!
In this challenge, you have a linked list and your task is to delete a specific node from it. You will be given a reference to the node which needs to be deleted, but you will not be given the head of the linked list.
Imagine you have a string of beads and you need to remove just one specific bead. You don't have the entire string, just the bead that needs to be removed. It's a little magical!
You have to modify the node referred to by the reference such that the node gets deleted from the list.
Here is how linked lists look:
A -> B -> C -> D
Suppose you need to delete B, your linked list should look like:
A -> C -> D
Important Note: You're not destroying the node itself, but shifting values. Assume nodes have a value and a reference to the next node.
Function Signature
def deleteNode(node: 'ListNode') -> None:
pass
Example
Assume you are given the node holding 2:
linked_list = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) node = linked_list.next # This is node "2" deleteNode(node) # Now, linked_list is effectively "1 -> 3 -> 4"
Your task is to implement the deleteNode function that alters the list as described.
How the tests work
Each test gives the list's values and the position of the node to delete (counting from 0). The list is built for you, and the test checks which values are left in it.
Inputs
Arg 1:
[1,2,3,4]Arg 2:
1Expected Output
[1, 3, 4]Actual Output