-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
Bug Report for https://neetcode.io/problems/remove-node-from-end-of-linked-list
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
While attempting to solve this problem, I decided to use an approach that reverses the LL, finds the correct element to remove, removes that element, and then reverses the LL again to return the original LL with the deleted integer. Here is the original solution I decided to implement.

However, as shown by the test case, my output returned null rather than a empty LL / empty node. The expected output is [ ], however, I couldn't figure out how to achieve that output value given the ListNode constructor defaulting values of a node to 0. After consulting ChatGPT and added what seems like an arbitrary edge case shown below, the output displayed corrects itself.
Fixed implementation:

As you can see, the specific test case where head = [5] and n = 1 now passes as the correct expected output of [ ] is now returned.
I believe this issue is occurring due to line 30 of the original implementation where prevPrev.next = cur.next. In this scenario using the first implementation, prevPrev will be set to null and using .next on this doesn't seem to throw any null exceptions. Instead, the code continues and returns an empty output rather than an empty ListNode.