Remove Node from Linked List

LINKED LIST

Problem

Given the head of a linked list and an integer target, remove the node with the value target from the linked list, if it exists. Ensure the linked list remains connected after the node is removed. If the target node does not exist in the list, leave the list unchanged.

Examples:

deleteNode([4,5,1,9], 5) // returns [4,1,9] // The value 5 is present in the linked list // so the node containing it is deleted. deleteNode([4,5,1,9], 10) // returns [4,5,1,9] // The value 10 is not present in the linked list // so there is no deletion.

Time Complexity

The optimal solution for this problem has a time complexity of O(n), where n is the number of nodes in the linked list.

Loading...
1
2
3
4
5