LeetCode problem #237: Delete node in a Linked List (JavaScript)

Duncan McArdle
1 min readJan 7, 2021

--

In this LeetCode problem, we’re asked to delete a node from a Linked List. However, we aren’t given the Linked List’s head, and are instead provided with the node that needs to be deleted.

To be honest, this question is a little too simple. Thanks to the question guaranteeing that you won’t be given the tail node of a Linked List, there aren’t even any edge cases to handle, so the solution comes in at a whopping 2 lines.

Solution #1: Replace the node with the next one

This incredibly simple solution simply replace the to-be-deleted node’s value with the next one in the Linked List, as well as the next value also. This technically deletes the next node in the list after copying its contents, but in doing so achieves the original goal of deleting the supplied node.

--

--

Duncan McArdle
Duncan McArdle

Written by Duncan McArdle

Full-stack software developer from the UK, author of the Aftermath book series, full time tech-nerd.

No responses yet