Skip to content

Bug Report for remove-node-from-end-of-linked-list #4463

@marniiii

Description

@marniiii

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.

Image

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:

Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy