so if this answer matters if we write fast != null we will see we have not breaked the linked list in two there is still connection between the 7->2 so that will not be null thats why we write slow != null as slow pointer will point to null 7->null (after the reverse of the list):)
Love your videos! Very well explained. When I tried this code though, I received a time limit exceeded error. I'm sure its on my end, but I'm not sure why because as far as I know its the same code and should work.
bro but for odd testcase it doesnt work .lets say we have a testcase 1->2->3->2->1->null according to you slow points to middle 3 , now if we reverse it will become 1->2->3->null ,where fast points to starting 1 .now if start comparing values @ fast and slow upto 2 it will be same after that slow points to 3 but fast points to null .so what to do?
Hello, what you can do: Traverse entire L. List. (Let Length of LList come out be n) Just push n/2 elements in the stack. Now you can check if n is odd which is your case. Curr will be pointing to -7. So Curr = Curr-> next; Now start comparing left-out L list with stack elements. If n was even. Eg 2 -3 -3 2 Then We would have done nothing. Coz We have already pushed n/2 elements. And Curr will be pointing to the Third element. Now start comparing left-out L list with stack elements. Hope it helps. 😄
bro trust me u r a god level teacher bro omg
i dont but i get a lot of clarity from your videos , Thanks a lot
explanation is awesome💥💥👍
Loved the explanation for both implementations! Thanks Nikhil!
Good explanation! Thank you for the great video 😃
thank you for subscribing.. :)
such a clean explanation, i loved it
your all explanations are awesome
Thank you so much 😀
Why not using collection linkedlist of java
Why we are making our own linkedlist ?
Why we are making our own tree?
Please answer
Y in his videos very less viewers😢
i am not getting ,what if the no.of nodes is odd ???pls explain
Not able to understand why i am getting an error when i write
while(fast!=null)
{
if(fast.val!=slow.val)
{
return false;
}
compare to the code I have
so if this answer matters
if we write fast != null we will see we have not breaked the linked list in two there is still connection between the 7->2 so that will not be null thats why we write slow != null as slow pointer will point to null 7->null (after the reverse of the list):)
When we are doing fast = head and traversed fast to null then why head is not null. Aren't we dealing with the address?
Love your videos! Very well explained. When I tried this code though, I received a time limit exceeded error. I'm sure its on my end, but I'm not sure why because as far as I know its the same code and should work.
try the code available on github (link in description)
i love it
bro but for odd testcase it doesnt work .lets say we have a testcase 1->2->3->2->1->null according to you slow points to middle 3 ,
now if we reverse it will become 1->2->3->null ,where fast points to starting 1 .now if start comparing values @ fast and slow
upto 2 it will be same after that slow points to 3 but fast points to null .so what to do?
It does work, did you try with such a case?
Nice explanation
ok,but what's in case of linkedlist of odd length?
The middle element does not matter.
1 4 x 4 1
The middle element x could be anything
when we do fast= head does it start from for head of ex 237732 this value or is it half
Can you please elaborate on your example?
please do a longest palindrome in a linked list
Will add it to my pipeline of video
Please make more explanation videos
Will do..new video every week 😄
Please share as much possible too 😎
The stack solution does not work if the pattern is 2 - 3 - 7 - 3 - 2. even tho it is a palindrome
go with the optimized one
Hello, what you can do:
Traverse entire L. List. (Let Length of LList come out be n)
Just push n/2 elements in the stack.
Now you can check if n is odd which is your case. Curr will be pointing to -7.
So Curr = Curr-> next;
Now start comparing left-out L list with stack elements.
If n was even. Eg 2 -3 -3 2
Then We would have done nothing. Coz We have already pushed n/2 elements. And Curr will be pointing to the Third element. Now start comparing left-out L list with stack elements.
Hope it helps. 😄
@@SaumyaSharma007 awesome thanks buddy i will give it ago.. :)