Why this is not giving correct output and runtime error? https://ide.codingblocks.com/s/340499 https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/ Please tell asap

Why this is not giving correct output and runtime error?


Please tell asap

Hey @cbcao263
Did 1 changes and mentioned that in comment :slight_smile:https://ide.codingblocks.com/s/340569

OMG, Literally making a lot of silly mistakes these days.
Thanks Sir for the help.

And Also What’s the best solution that you can think of it?
As I made first general solution then have some changes according to the edge cases?

1 Like

Hey
Add dummy node in front
Take 2 pointers pointing to dummy and move 2nd pointer k+1th times ahead(k is the node to be deleted from last)
now move both pointers until 2nd pointer reaches null
Now u have to remove the next of first pointer
so do

temp=curr->next;
cur->next=temp->next;
delete temp

and finally return dummy->next(removing dummy node)

Okay Sir, I also have applied the same logic for finding that node.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.