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

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Duncan McArdle

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