Member-only story

LeetCode #237 — Delete Node in a Linked List

David Seek
2 min readJun 2, 2020

--

Find more useful articles at www.davidseek.com

LeetCode’s challenge of June 2, 2020 (#237) asks us to delete a Node from a LinkedList. The tricky part is that we only have access to the Node itself, not to the head or parent Node.

Given the Node 5, as part of the LinkedList (4->5->1->9), we're expected to return (4->1->9) with the Node 5 cut out of the list.

Admittedly, the question is rather confusing. One would expect access to the head and a target value to find and cut out. However, that’s not the case. So, we need to dig deeper into the problem and realize that given an object reference, we don’t need any iteration or further access to the list.

All we need to do is to assign next.val of the given node to val and next.next to next of the node itself. Lastly, because it's Swift, we need to make sure to obey to the rules of handling optionals properly.

Find more useful articles at www.davidseek.com

--

--

David Seek
David Seek

Written by David Seek

Software Engineer at Amazon (Alexa Mobile)

Responses (1)