Тёмный

LeetCode Palindrome Linked List Solution Explained - Java 

Nick White
Подписаться 377 тыс.
Просмотров 111 тыс.
50% 1

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow My Twitter - / nicholaswwhite
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Наука

Опубликовано:

 

31 дек 2018

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 121   
@tiyasghoshroy9577
@tiyasghoshroy9577 3 года назад
Odd-numbered items example Say the original list is 12321. After iterating through slow and fast: Slow -> 321 After reversing slow: Slow -> 123 Reset fast to head However, note that the state of the original list is now: fast->1->2->32->3 BUT 1->2->3
@awesomebearaudiobooks
@awesomebearaudiobooks 8 месяцев назад
Do you mean to say that the solution he provided would work for both the even-numbered and the odd-numbered examples? I thought we should maybe just reverse the entire list and then compare both lists... That would still be O(n) time complexity, even though it will be twice as slow... But if his solution works for odd-numbered lists, then great! :D
@ssdoesntlie
@ssdoesntlie 5 лет назад
Maybe you can put the camera window to the right top corner? It blocked the code.
@palashkhatri7820
@palashkhatri7820 4 года назад
absolutely
@nguyenquangtri2224
@nguyenquangtri2224 3 года назад
I think it should be at the left bottom corner
@sadmanabedin1203
@sadmanabedin1203 3 года назад
0:01, that slapping which every Nick White video starts with.
@kevindai1000
@kevindai1000 3 года назад
I have been watching a lot of the videos that you posted on solving leetcode problems, cuz i have no idea how to do a lot of them on my own, lol. Just want to say thank you for spending the time to make these videos and posting them on youtube, it really helps!!!
@gautamarora6516
@gautamarora6516 3 года назад
Great Man! You are literally helping the community a lot. Keep it up!
@vitalijdao724
@vitalijdao724 4 года назад
Thank you Nick!! Really good explanation!!
@garrett-ohara
@garrett-ohara 3 года назад
Thanks for this tutorial, i just didnt understand why you needed to go to half of the array. But once you reverse it, it all makes sense.
@mohammedfalih8713
@mohammedfalih8713 3 года назад
great explain .. keep up the good content .. I think you should also put a pointer to the camera window and iterate it to the left corner
@saiavinashduddupudi8975
@saiavinashduddupudi8975 4 года назад
Awesome explanation! Thanks Nick
@anushkadhar7595
@anushkadhar7595 5 лет назад
Hey you explained it really nice could you load more problems.
@theSDE2
@theSDE2 3 года назад
My Goal for this year is to solve all the leetcode problems you have done here. Your channel is the first place I refer to if i am stuck solving LC.
@barista_code
@barista_code 8 месяцев назад
How do you going?
@vijiisasi
@vijiisasi 4 года назад
awesome explanation nick
@SudeepDasguptaiginition
@SudeepDasguptaiginition 4 года назад
We can also make a copy of the original list, reverse the list and then compare the original list and the reverse list using a loop ..
@woljitsuu8974
@woljitsuu8974 3 года назад
that doesn't take O(1) space dumbass...
@MaminaZvezdochka
@MaminaZvezdochka 3 года назад
Thanks so much for all🍀
@marcbatete9003
@marcbatete9003 4 года назад
very good stuff Nick, this is extremely helpful. and dont worry none of us CS guys are good at explaining, you're doing good.
@codefire1128
@codefire1128 4 года назад
nice one!! but, try to provide the source code as you're window always blocks the screen
@ramyamurthy5202
@ramyamurthy5202 3 года назад
When there is a odd length linked list, we should not compare the middle element with anything. For ex: 11211 is a palindrome and the value "2" which is a middle element shouldn't be a part of the comparison logic. This logic needs to be added here.
@batkhuyagochirkhuyag7651
@batkhuyagochirkhuyag7651 3 года назад
I also think same at first but it doesn't matter whether it's odd or even length list since we are comparing it to the original list after reversing it. So it will compare reversed list 112 with original list 11211 which will iterate until end of reversed list.
@cynthiayyyang9667
@cynthiayyyang9667 3 года назад
@@batkhuyagochirkhuyag7651 nice
@tiyasghoshroy9577
@tiyasghoshroy9577 3 года назад
Say the original list is 12321. (odd-numbered items example) After iterating through slow and fast: Slow -> 321 After reversing slow: Slow -> 123 Reset fast to head However, note that the state of the original list is now: fast->1->2->32->3 BUT 1->2->3
@sidharthsinghal5543
@sidharthsinghal5543 2 года назад
@@tiyasghoshroy9577 so how will the while(slow!=NULL) condition be true? as slow is pointing the end of the Linked list, when will slow be equal to NULL, definitely not in the middle right? as right half is connected to the left half.
@dadingchen8323
@dadingchen8323 2 года назад
if you are not feeling comfortable with the case, you can simply add fast!=null to the while statement if you want to. It would be like while(fast!=null && slow!=null)
@yuvrajbansal6294
@yuvrajbansal6294 4 года назад
Great job
@ashwinvarma9349
@ashwinvarma9349 3 года назад
bro, can u share the code? the camera window blocked the code
@guguls
@guguls 3 года назад
is it possible to start posting ur code on git?
@saidharshanshan2481
@saidharshanshan2481 2 года назад
wow, this solution is so genius. Algorithms are fun
@HenggaoCai
@HenggaoCai 4 года назад
Your camera window is blocking the code. Can you move it?
@amirkhan355
@amirkhan355 3 года назад
Love your videos
@christophersimcik6595
@christophersimcik6595 4 года назад
Thanks!! Awesome solution. Could you also use a stack to reverse the order of the first half and compare the second half to the "popped" value, rather than reverse the list?
@christophersimcik6595
@christophersimcik6595 3 года назад
@Stefan Imig :) good point oops
@sourav_ghosh
@sourav_ghosh 4 года назад
Basically, i copied the same concept and wrote in python, the submission was wrong. However when i wrote the same thing in java, it accepted
@amrholo4445
@amrholo4445 4 месяца назад
Thanks a lot
@KaisarAnvar
@KaisarAnvar 2 года назад
Bro you’re such a coding gangsta 😎 I hope I will be able to at least THINK of the logic before writing the code.
@MegaMurimi
@MegaMurimi 2 года назад
I didn't see the reverse bit so I wrote my own code. But thanks for the solution!
@srinimurthy
@srinimurthy 2 года назад
Thanks for covering most of the code with your image 🙂
@vinayak186f3
@vinayak186f3 3 года назад
I wrote similar code in c++ it shows better than only 23.44 percent ! Why so ?
@ottyudo7208
@ottyudo7208 4 года назад
when the slow pointer gets to 3, where would the faster pointer be, null?
@rajatsemwal1865
@rajatsemwal1865 3 года назад
Yes. The 'fast' pointer will be pointing to null. It was used just to help 'slow' pointer to reach the middle of the list, so that the other half of the list could be reversed later. I hope you understand.
@sharathm710
@sharathm710 4 года назад
For those who cant see last function :) public ListNode reversed(ListNode head)
@rajatsemwal1865
@rajatsemwal1865 3 года назад
You are going to heaven for this bro!
@AkashSingh-tw3uv
@AkashSingh-tw3uv 3 года назад
people who haven't guessed it, why are they even here ?
@nandiniverma5273
@nandiniverma5273 2 года назад
thanks a lot
@playplus893
@playplus893 2 года назад
i mean personally i will traverse the linked list and store the values in an array and then reverse the array and make it a string and integer and compare both arrays or so ...although it might be slower
@jongeorge3358
@jongeorge3358 Год назад
would be slower and use more space.
@yawar110
@yawar110 2 года назад
would not it be easier to copy the original linked list and reverse it, then compare the two?
@gregorykirchoff6118
@gregorykirchoff6118 2 года назад
That is a viable solution and how I solved it at first. Although, that has a space complexity of O(n) instead of O(1) because you have to create an array, which will grow in size proportionally to the length of the linked list.
@atharvakulkarni2024
@atharvakulkarni2024 3 года назад
suggestion-> YOU CAN USE WHITEBOARD TO EXPALINA ND THEN CODE
@Augustus1003
@Augustus1003 4 года назад
oh lord,I took queue data structure to put given list into queue,then i reversed the list and iterated through each node checking if val are equal or not.
@tonyz2203
@tonyz2203 4 года назад
I think it’s just easier to use stack since you don’t need to reverse the linked list.
@natasaivic8845
@natasaivic8845 Год назад
Hi Nick, Your work is truly appreciated, I learned a LOT from your content. I find that your approach to presenting information is in line with my own way of thinking, making you the ONLY RU-vidr who has been able to resonate with me in this manner. Since this question is super important and it is frequently asked, can we discuss a better solution than this? Although it is accepted by LeetCode, the interviewer would say that my Linked List got broken, I could offer to re-reverse the second half and put the list back together. But the interviewer would further note that the proposed solution is not suitable for a concurrent environment, where multiple threads or processes may concurrently access the same data. And for sure, one potential disadvantage of modifying a Linked List is that it can temporarily disrupt the structure of the list, which can cause issues if other threads or processes try to access the list at the same time. One answer to avoid such issues can be to use locks or other synchronization mechanisms to ensure that only one thread or process can access the list while it is being modified. But the interviewer will confirm that this can add additional overhead and complexity to the code. SUMMARY: a nightmare :D Can you discuss O(1) space complexity other than modify the list in place? I would much appreciate it if you get back to me. Best, Nataša
@rupaldesai7098
@rupaldesai7098 4 года назад
odd number of elements then we need to increase slow = slow.next right? before we reverse
@user-mn3iq2cs9n
@user-mn3iq2cs9n 4 года назад
I'm also wondering how to deal with an odd number palindrome.
@Roshid-zy5cb
@Roshid-zy5cb 4 года назад
In the while loop do: if(fast.next != null) slow = slow.next; if(fast.next != null) fast = fast.next.next; else fast = fast.next;
@drishyapremchandani6438
@drishyapremchandani6438 4 года назад
so all the test cases only have even number of elements ?, as his code passed
@NoodGaming
@NoodGaming 4 года назад
I think you could just change line 26 to while (fast != null)
@rct3vids99
@rct3vids99 4 года назад
@@NoodGaming The slow pointer stops at index (n+1)/2 so the code works for both odd and even number of elements.
@rohanprak
@rohanprak 5 лет назад
thanks bro greeting from INDIA
@CrazyHunk14
@CrazyHunk14 4 года назад
and what about the linked list which are of odd length like 1-2-1. should't it be while(fast!=slow && slow!=null) on line 29????
@manojkr9198
@manojkr9198 2 года назад
I think The code would work fine for this case too... Cuz at the end, both fast and slow pointer would point to null
@alokkothiyal9845
@alokkothiyal9845 3 года назад
its failing for 1->0->1
@okey1317
@okey1317 3 года назад
i am finding this problem really difficult. Though i see it listed as Easy in leetcode :(
@Ashish-wz3it
@Ashish-wz3it 2 года назад
are you any good now?
@okey1317
@okey1317 2 года назад
@@Ashish-wz3it 😁i forgot what this was abt, posted 8months ago
@Ashish-wz3it
@Ashish-wz3it 2 года назад
@@okey1317 🙂I meant are you a good coder now? after 8 months...
@okey1317
@okey1317 2 года назад
@@Ashish-wz3it Better, But i admit I wasnt very consistent. I havent opened leetcode in many months. I just realized how good i would have been now even if i had solved 1problem per day. Bad me😑 Lazyness and procastination is killing me
@Ashish-wz3it
@Ashish-wz3it 2 года назад
@@okey1317 This happens to everyone...and its never too late.
@surendrapeatihar2650
@surendrapeatihar2650 4 года назад
it may be possible by using stack? //.....
@kevinz6097
@kevinz6097 4 года назад
Stack requires O(n) space complexity. The question has a follow-up section that asks the solution to use O(1) space.
@omprakashgaini8007
@omprakashgaini8007 2 года назад
your camera window is blocking the code
@pinkkitty6553
@pinkkitty6553 Год назад
bro why is this problem in the easy category
@sahilajmera1290
@sahilajmera1290 5 лет назад
Will this work for 1->2 ? I think it needs minor modifications.
@user-mn3iq2cs9n
@user-mn3iq2cs9n 4 года назад
It must work or it wouldn't pass here. I wrote basically the same code but didn't call the method, which I thought just made it less neat, but I'm not getting false for the 1->2 testcase. I don't know why......
@FlexerPivot
@FlexerPivot 4 года назад
it works because slow will move up to 2, and fast will stay at 1. With this logic, that is how it works, I've debugged as well. is 2 == 1? Nope, so it returns false. Hope this helps :)
@filipzdravkovic8557
@filipzdravkovic8557 Год назад
omg the new mic
@CoderAdi
@CoderAdi Год назад
idk about this some 3 years before but now this solution is showing TLE :
@Kalaivani-qr1vc
@Kalaivani-qr1vc 4 года назад
what if the number of nodes is odd ?
@kaichen6288
@kaichen6288 4 года назад
take an example like 1->2->3->2->1. after reverse, it looks like 1->2->33. slow: 1->2->3. both have same node with val 3.
@prarthanakandwal6625
@prarthanakandwal6625 4 года назад
@@kaichen6288 But after reversal, the list would be 1->2->1->2->3
@raaghaviravisankar1853
@raaghaviravisankar1853 3 года назад
If the nodes are odd then it will be an issue because slow will be in the middle which we need not compare
@nevikgnehz368
@nevikgnehz368 2 года назад
@@kaichen6288 Agreed because the node prior to the middle is still pointing to the middle
@mdouet
@mdouet 3 года назад
I might be over simplifying this, but couldn't we just store each value in the LinkedList in an ArrayList (in order), call Collections.reverse() and save it as a new ArrayList, and then compare the two lists?
@biswajitchanda3701
@biswajitchanda3701 3 года назад
Yes, u can do that. But the problem is most of the big product companies will not allow you to use built-in data structure like ArrayList etc.
@mdouet
@mdouet 3 года назад
@@biswajitchanda3701 I've interviewed with several FAANG companies and never once was I told I couldn't use built in data structures.
@mayankgm6520
@mayankgm6520 2 года назад
using arraylist the space complexity would be O(N) whereas doing with the given approach, the space complexity is O(1) so its better.
@jonsantos6056
@jonsantos6056 2 года назад
Good video man but what the hell your whole potshot is blocking the bottom part of the code man.. jeez
@rashedulislam9301
@rashedulislam9301 4 года назад
why this code shows TLE. Thanks in advance. class Solution { public boolean isPalindrome(ListNode head) { String s1,s2; s1 = ""; s2 = ""; while(head != null) { s1+=String.valueOf(head.val); s2=String.valueOf(head.val)+s2; head = head.next; } return s1.equals(s2); } }
@Xellos976
@Xellos976 4 года назад
string addition is a linear time operation so the running time of your algorithm is quadratic (n^2)
@rashedulislam9301
@rashedulislam9301 4 года назад
@@Xellos976 thanks
@ridj41
@ridj41 Год назад
Definitely a medium problem
@hassanqureshi773
@hassanqureshi773 Год назад
Why isn’t a tail here?
@PorterPickUp
@PorterPickUp 8 месяцев назад
Why not just reverse the linked list then run through the length of the original and reversed list comparing as you go? Both values have to be identical for it to be a palindrome.
@Jack-ss4re
@Jack-ss4re Месяц назад
would work too but then you consuming 2 times more memory than it should
@abhinavminocha1894
@abhinavminocha1894 4 года назад
you should have been loud to understand what you saying
@alokkothiyal9845
@alokkothiyal9845 3 года назад
its failing for 1->0->1 As after first while loop the slow = 0 so then fast = 1 1!=0 return false but 101 is a palindrome
@arpitsatnalika
@arpitsatnalika 3 года назад
Not failing for me
@tando8743
@tando8743 3 года назад
slow at 0 => reverse will be 1 -> 0 = fast 1 -> 0
@davidkennedy8838
@davidkennedy8838 4 года назад
This solution is convoluted and includes recursion, which is not accepted by big tech companies. There is no reason to reverse half. Instead, stick the first half in a stack, then compare the second half to the entries in the stack.
@araniel
@araniel 4 года назад
That makes O(n) memory complexity. The solution on the video is still O(1);
@nvsabhishek7356
@nvsabhishek7356 4 года назад
This will make O(n) memory. We have to find O(1) memory and O(n) time solution
@syakov06
@syakov06 3 года назад
Why not to use a 2 pointer technique? You just run to the end with b pointer and then compare the elements. a+ + b- - Need to adjust stop condition but it seems to me easier.
@Blank-mw8ob
@Blank-mw8ob 3 года назад
In this problem, there's only a next property, not a previous property, so you can't do b--;
@mayankgm6520
@mayankgm6520 2 года назад
you cant go directly in a reverse direction in linkedlist
@rutvipatel4307
@rutvipatel4307 3 года назад
It destroys the structure of the linkedlist. Not a good appraoch when working on practical projects.
@CronusVelox
@CronusVelox Год назад
That's why this is a leetcode problem. You could always restore it if you want
@floramerano6293
@floramerano6293 Год назад
Should've just used a visualization.
@vigneshnatarajan485
@vigneshnatarajan485 Год назад
Coding Skills: 10/10 Teaching Skills: 3/10
@shubhampatil5935
@shubhampatil5935 2 года назад
ANY Indian channel will explain more clearly than this guy.
@ita746abhishekkhairnar5
@ita746abhishekkhairnar5 Год назад
actually the main problem is , he isn't presenting the steps to solve the problem, rather than his imagination, he should use a white board. I often get confused by his explanation because I'm a newbie in data structures.
@dipakkumarrai1442
@dipakkumarrai1442 Год назад
your video is hiding the code :)
@scm534
@scm534 10 месяцев назад
the vid hiding the most important part of the code is torturing me
Далее
LeetCode Pascal's Triangle Solution Explained - Java
9:20
Stay on your way 🛤️✨
00:34
Просмотров 8 млн
LeetCode Reorder List Solution Explained - Java
12:55
Palindrome Linked List - Leetcode 234 - Python
11:06
Просмотров 91 тыс.
LeetCode - Reverse Linked List Solution
7:02
Просмотров 122 тыс.
I Got Rejected (again)
9:43
Просмотров 203 тыс.
LeetCode Sort List Explained - Java
9:29
Просмотров 78 тыс.
НЕ БЕРУ APPLE VISION PRO!
0:37
Просмотров 287 тыс.
Копия iPhone с WildBerries
1:00
Просмотров 7 млн