educative.io

Removing the head node

I’m still not exactly sure why we should remove the head node of the linked list when the right pointer node becomes null when moving the right point forward nth positions.

If the right pointer reaches NULL while moving it n steps forward, it means that the head node should be removed. We return the head's next node.

Any ideas why we remove the head node and not return it instead?

Hi @Tayte_Stokes,
could you please share the lesson link?

Thanks!

Hey @Komal_Arif,

Sorry about that, here is the link! Solution: Remove nth Node from End of List - Grokking Coding Interview Patterns in JavaScript

1 Like

Hi @Tayte_Stokes,

Thank you for reaching out.

The goal of this problem is to remove the nth node from the end of the linked list and return the head of the updated linked list. Therefore, in a case where the right pointer becomes NULL, it means that the node to be removed is the head node. In this case, we will remove the head node and return the new updated head, which will be the node immediately following the head node (head.next). This approach simplifies the implementation and ensures that the modified list remains valid.

If you have any more questions or need further clarification, please let us know.

Happy learning!

1 Like