At 3:57, you said fast is at 4, I think this is incorrect. Fast should be at 3 because it starts from 0 which is the dummy head. At that moment, slow should be at the beginning which should be 0, not 1.
We just need to go to the node before the one to be removed and to maintain the distance n from last we can take two-pointers and maintain the same distance between them, when one reaches null other reaches the required position and gets the job done
Hey Nick, I solved this problem with a similar method. But it was written there that we should do it in one pass. So, by having a fast and a slow pointer, arent we making 2 passes to the linked list?
would it be possible to do this with a stack? if you pop all the nodes in, once node->next == nullptr, you could then pop the n times, store the toDelete->next as a temp, and the node left on the top would be the prev node which you can set next to delete's next? Or would this not actually work?
it is somewhat arbitrary I've gotten 30% faster and then 100% with the exact same code just running it different times in leetcode. Don't worry too much about the percentage
I solved this by traversing the whole list and at each point, I appended the node to an array. At the end, you get a an array containing pointers to each node of the list, and I just fetched the (len(arr) - n)th node from the array and deleted it. That's one pass and without using two pointers. Why doesn't anyone talk about this solution?
Yep, you can do this with a single while loop, with a condition , start a counter, from 0 and when the value is greater than n+1 start moving the slow pointer and fast pointer else only move the fast pointer.
At 3:57, you said fast is at 4, I think this is incorrect. Fast should be at 3 because it starts from 0 which is the dummy head. At that moment, slow should be at the beginning which should be 0, not 1.
That was what confused me for a long time.
Yes that is correct - He made a mistake!
I think he meant that in the linked list the value starts at 1, but the index is 0. so the index is at 3, but the value of the linked list is 4.
Thanks. I was confused about it
thank you, it helped me too, was bugging me for the whole day!
If n== length of list then we will get null pointer exception for your solution
i think if Nick explain it with 0 node in the video would be more understandable because the actual node in the solution is appended with a new node 0
These leetcode problems really help a lot. We appreciate it!
I understood the code, but can you please explain the logic(the reason behind doing it this way) in words. That would be really helpful.
We just need to go to the node before the one to be removed and to maintain the distance n from last we can take two-pointers and maintain the same distance between them, when one reaches null other reaches the required position and gets the job done
@@sarthakbhatia7639 Well explained
@@sophiezhang6516 Thankyou
@@sarthakbhatia7639 Excellent explanation. This should be in the video description. Kudos
@@sarthakbhatia7639 Really good explanation. This is just so simple.
I have used a different method:
step1: Reverse the whole linked list.
step2: Traverse using count variable to print nth node.
I too thought the same approach, but is it considered as one pass solution?
@@suganyam1672 no that's two pass
doesn't work when size of the list is same as n. simple if statement solves it but just pointing it out.
works for me no if statement needed
Hey Nick, I solved this problem with a similar method. But it was written there that we should do it in one pass. So, by having a fast and a slow pointer, arent we making 2 passes to the linked list?
you are
Hey why did you put 0th node in dummy node why not start the slow pointer and fast pointer from head instead??? Please explain
This is just to prevent edge cases. For example, if the list provided has only 1 node, pointing to the head directly will be wrong.
can we use temp instead of dummy_node ?
It's just a name. Do whatever name make sense to you/reader.
0:14 - 0:17 '"And all that other crap" lmaooooo
Superb your explanation is engaging and good
why to use dummy?
can we use temp instead?
@@tekken1935 yes you can use temp
the loop should run to n-1, not n+1. Try it with n+1 and see it failed on test data.
would it be possible to do this with a stack? if you pop all the nodes in, once node->next == nullptr, you could then pop the n times, store the toDelete->next as a temp, and the node left on the top would be the prev node which you can set next to delete's next? Or would this not actually work?
sounds like it would work, did you try it out?
What if the value one enters is the the value at the beginning
I have the same doubt. I tried the same in JavaScript, It broke when i wanted to delete the first value
hey the discord is expired?
premium problems on patreon? really...
how can u always get better than 100% of the solution ??
it is somewhat arbitrary I've gotten 30% faster and then 100% with the exact same code just running it different times in leetcode. Don't worry too much about the percentage
The legend Nick White
I solved this by traversing the whole list and at each point, I appended the node to an array. At the end, you get a an array containing pointers to each node of the list, and I just fetched the (len(arr) - n)th node from the array and deleted it.
That's one pass and without using two pointers. Why doesn't anyone talk about this solution?
Because this array of yours uses extra space , which is not needed , buddy
@@adityapandey8245 chill out with the buddy
@@HialeahLiam stfu buddy
Is it possible to do it in one loop?
This was done in one loop.
Yep, you can do this with a single while loop, with a condition , start a counter, from 0 and when the value is greater than n+1 start moving the slow pointer and fast pointer else only move the fast pointer.
master luke????
thanks nick
ALWAYS OP
niceeeeeeeeeeeeeee
😯😯😯
oneth
not good!