Palindrome Linked List (LeetCode 234) | Full solution with trick | Study Algorithms
Vložit
- čas přidán 23. 07. 2024
- A sequence could be represented using a string, array or even a linked list. A given list will be a palindrome if we get the same order when traversing from head to null, and null to head. This can be done by using a stack data structure and if space is an issue, we can also modify the actual list to determine the palindrome.
Actual problem on LeetCode: leetcode.com/problems/palindr...
Chapters:
00:00 - Intro
01:03 - Problem Statement and Description
02:37 - Solution using Stacks
07:24 - Space Optimized Solution
10:49 - Dry-run of Code
13:24 - Final Thoughts
📚 Links to topics I talk about in the video:
Linked List: • Linked List Data Struc...
Traversing Linked List: • Traversing a Linked Li...
Reverse a Linked List: • HackerRank - Reverse L...
Stack Data Structure: • Stack Data Structure e...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
Loved the explanation for both implementations! Thanks Nikhil!
such a clean explanation, i loved it
i dont but i get a lot of clarity from your videos , Thanks a lot
explanation is awesome💥💥👍
Good explanation! Thank you for the great video 😃
thank you for subscribing.. :)
your all explanations are awesome
Thank you so much 😀
i love it
Why not using collection linkedlist of java
Why we are making our own linkedlist ?
Why we are making our own tree?
Please answer
When we are doing fast = head and traversed fast to null then why head is not null. Aren't we dealing with the address?
Nice explanation
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)
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?
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):)
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?
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 😎
Y in his videos very less viewers😢
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.. :)