educative.io

Https://www.educative.io/courses/grokking-coding-interview-patterns-java/solution-remove-nth-node-from-end-of-list

  1. Linked List: 23 → 89 → 10 → 5 → 67 → 39 → 70 → 28 → null
    n = 40
    Updated Linked List:

Exception in thread “main” java.lang.NullPointerException
at RemoveNthNode.removeNthLastNode(main.java:10)
at RemoveNthNode.main(main.java:46)

1 Like

Hello @Rishabh_Purwar,

The compiler is throwing a null pointer exception because there doesn’t exist any node at n = 40. The total number of nodes in the given linked list is 8. It’s worth noting that the test case violates a problem constraint, as it is specifically mentioned that the total number of nodes in the list is k, and k is always greater or equal to n which is the nth node that we want to remove from the linked list.

To resolve this issue, you should adjust the value of n to fall within the valid range of nodes in the linked list, which is from 1 to 8.

I hope this clarifies the concern you raised about the problem. If you have any suggestions or feedback, please feel free to share. We are here to assist you.

Happy learning!