Add Two Numbers (LeetCode 2) | Full solution with diagrams | EASIEST EVER | Study Algorithms

  Рет қаралды 62,970

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 133
@sharpblades117
@sharpblades117 Жыл бұрын
Just want to say, thank you for covering this in the clean format that you did, this made the video engaging. Keep up these amazing videos.
@johncho9160
@johncho9160 Жыл бұрын
i really enjoy your leetcode explanations with the dry run of the code. as somone prepping for interviews, youve helped me a lot. thankyou!!!
@anujgupta1484
@anujgupta1484 Жыл бұрын
you are truly a gem for DSA learners .
@sachinpawar2665
@sachinpawar2665 Жыл бұрын
Your leet code solutions are above the par and the explaination is the deadly combination
@nikoo28
@nikoo28 Жыл бұрын
So happy to hear that :)
@preetomadityapranoy429
@preetomadityapranoy429 Жыл бұрын
Thanks a lot for this simple and easy solution. I solved the problem but test case 1565 out of 1568 was my stuck point and after that, I found your video. Keep up the excellent work.
@nikoo28
@nikoo28 Жыл бұрын
Well done
@shubhanshusharma7457
@shubhanshusharma7457 Жыл бұрын
nice explanation, got understood the solution in one video of yours,despite searching others
@nikoo28
@nikoo28 Жыл бұрын
glad you found it helpful
@naibgulam2336
@naibgulam2336 2 жыл бұрын
Really enjoyed this video! Been following your channel to learn how to solve leetcode questions and your videos and organization are super useful and well thought out. Keep it up!
@nikoo28
@nikoo28 2 жыл бұрын
You're very welcome!
@itkavyas.p6176
@itkavyas.p6176 Жыл бұрын
i just started DSA as beginner after watching most of the solution your solution is very simple and easy please post more videos and support peoples ..... you earned the new subscriber
@nikoo28
@nikoo28 Жыл бұрын
thanks for the sub 😄
@EaseyourLifeBro
@EaseyourLifeBro 8 ай бұрын
You deserve many more subscribers 😃Concise and accurate explanation. No confusion, every point makes sense. Please keep up the good work. 👍
@nikoo28
@nikoo28 8 ай бұрын
Trying hard 😇
@juli13585
@juli13585 Жыл бұрын
Really easy to understand explanation and the best I have seen for this problem, thank you so much!
@safar19899
@safar19899 Жыл бұрын
Your solution made the most sense to me. Thanks!
@JigCode
@JigCode Жыл бұрын
This is the best and clearest explanation of a leetcode problem that I have ever seen !!!
@gamersrock392
@gamersrock392 Жыл бұрын
i really appreciate you sharing solution and telling how converting linked list into integers then adding and then again converting into linked list method is inefficient as thats what came in my mind while first solving solution
@nikoo28
@nikoo28 Жыл бұрын
glad you found it helpful
@Bot__gam_ing
@Bot__gam_ing Жыл бұрын
bruh you are awesome not even get bored while watching thanks
@zeus4963
@zeus4963 Ай бұрын
Probably the best explanation!. Thanks buddy
@VasundhraMinhas
@VasundhraMinhas 3 ай бұрын
I came across this amazing video. Keep up the good work!
@PrashantKumar-fk8le
@PrashantKumar-fk8le Жыл бұрын
This man explains Best of best on youtube,Huge Salute 👑👑👑👑
@nikoo28
@nikoo28 Жыл бұрын
so glad you feel this way.
@Uma_she
@Uma_she 6 ай бұрын
You teaching way like hits deeply registered Thank you nikhil😊
@PremkumarIT
@PremkumarIT 9 ай бұрын
very clear explanation brother ..I so great to find your videos
@mohdyusuf9694
@mohdyusuf9694 Ай бұрын
i didn't understand it was this difficult until i watched ur video!
@iosjumpstart
@iosjumpstart 5 ай бұрын
dude I really appreciated the explanation, well done, thanks
@KRajesh-ej7dy
@KRajesh-ej7dy Жыл бұрын
I Felt Happy After Listening This Thank You Bro
@deveshvyas3633
@deveshvyas3633 3 ай бұрын
Excellent Explanation 😀
@이영진-r7e
@이영진-r7e 7 күн бұрын
the best explanation!!
@krishankantsaraswat2276
@krishankantsaraswat2276 7 ай бұрын
00:02 Adding two numbers in linked list format with reversed order 01:39 Adding two reversed numbers represented as linked lists. 03:27 Handle zeros at the beginning of linked lists while adding numbers. 05:06 Carefully consider edge cases for adding large numbers and handling overflow. 06:45 Adding two numbers using pointers and carrying over. 08:23 Code implementation and algorithm explanation 10:02 Splitting out the sum and carry after adding two numbers 11:47 Understanding the time and space complexity of the solution Crafted by Merlin AI.
@sachinpawar2665
@sachinpawar2665 Жыл бұрын
Amazing content bro keep going, only time is your enemy, just wait for some time and you will reach to new heights.
@nikoo28
@nikoo28 Жыл бұрын
Hope so
@TheA1facts
@TheA1facts 8 ай бұрын
Good work, Keep it up!
@rajeevkumarshetty4911
@rajeevkumarshetty4911 Жыл бұрын
super broo this is one of the best and easy solution i have ever seen thank you so much for this solution its really helpful🤩🤩😍😍👏👏
@IshaZaka
@IshaZaka 4 ай бұрын
plz make hard level of leetcode as well, your explanation is so good
@Anuli-Edom
@Anuli-Edom Жыл бұрын
Best explanation to this i have seen so far
@nikoo28
@nikoo28 Жыл бұрын
yay!!
@cookiebrown3405
@cookiebrown3405 11 ай бұрын
this was a beautiful explanation. Thank you
@mohammedirfanu8180
@mohammedirfanu8180 Жыл бұрын
You are really teaching amazing sir, I can undestand very clearly❤
@bashlaev_ka
@bashlaev_ka Жыл бұрын
Частично понимал перевод, но за счет очень классного визуального объяснения понял объясняемый алгоритм. Круто, спасибо, было интересно
@nikoo28
@nikoo28 Жыл бұрын
Я так рада, что смогла вам помочь.
@saitejasrirambhatla248
@saitejasrirambhatla248 Ай бұрын
Can you plese tell me why do we need another node ptr here rather than working on result node
@adityashinde7815
@adityashinde7815 Жыл бұрын
Thank you brother your explanation is amazing . i understood this problem very clearly
@alexpa24
@alexpa24 Жыл бұрын
Very well explained. Thanks
@nandanupadhyaya4192
@nandanupadhyaya4192 Жыл бұрын
Beautiful explanation..🔥🔥
@kalairoxan2398
@kalairoxan2398 Ай бұрын
Please Do clarify the Constraints if it is like concoction
@lil_paha
@lil_paha Жыл бұрын
Thank you ! You make best explanations :)
@ToireasaMoley-mk3tw
@ToireasaMoley-mk3tw Жыл бұрын
great explanation! so easy to follow :)
@natnaeldesta5932
@natnaeldesta5932 Жыл бұрын
explained simply! thank you
@nikoo28
@nikoo28 Жыл бұрын
Glad it was helpful!
@IshaZaka
@IshaZaka 4 ай бұрын
what drawing tool you use to draw the diagrams? your way of explaning is so good
@evliyabilmemkimhazretleri9811
@evliyabilmemkimhazretleri9811 11 ай бұрын
thanks for the explanation.
@joaoguilherme-or1ud
@joaoguilherme-or1ud 8 ай бұрын
Muito Obrigado por essa Explicação fantastica !!!
@chakravarthybatna1589
@chakravarthybatna1589 Жыл бұрын
awesome explanation bro
@reddibabu7415
@reddibabu7415 3 ай бұрын
Thank u sir , subscribed
@naveennaidu4855
@naveennaidu4855 Жыл бұрын
sir suppose in the last Iteration carry is more than one than what happens. in your code how can you decide carry is one... ?
@nikoo28
@nikoo28 Жыл бұрын
when you are adding 2 single digits, the carry can never be more than 1 :)
@armaan8449
@armaan8449 10 ай бұрын
I had the same question but, two single digits can't have carry more than 1
@SAIKISHORKARRI
@SAIKISHORKARRI 8 ай бұрын
why we should add sum in the ptr.next?in think we should add in ptr.next.val ?
@nikoo28
@nikoo28 7 ай бұрын
can you tell me the timestamp you are referring to?
@ilmanti
@ilmanti Жыл бұрын
Great explanation! Could you please explain how result contains the numbers if we're only assigning the numbers in the next for the ptr?
@nikoo28
@nikoo28 Жыл бұрын
the result does not contains numbers, it contains a linked list, that has the answer numbers. That is what the question asks as well :)
@ilmanti
@ilmanti Жыл бұрын
@nikoo28 I understand, I used nunbers by mistake. What I meant is how does it contain the full linked list if we're only updating the ptr linked list? Hope that clears up the question!
@nikoo28
@nikoo28 11 ай бұрын
It still has single digits, you will have to interpret/construct the number from the list
@SinchannDekesuki
@SinchannDekesuki 7 ай бұрын
i have a doubt in this question i was able to solve this problem without using any extra space after that i saw many sloution just to know different approach most of them are creating new linked list which is extra space i have stored my result in the linkedlist which has max length of question it is right to do so ? or i shouldn't modify the question's linked list..
@jmajid774
@jmajid774 7 ай бұрын
Thanks. very cool video
@pathumkumara4696
@pathumkumara4696 24 күн бұрын
thank you so much
@jjoelthomas
@jjoelthomas 11 ай бұрын
Great tutorial. Just a small suggestion. int sum = 0 + carry; Is this needed? If 0 is going to be added every time in the loop we can just ignore it and write it like int sum = carry. I tried and it worked. Only if we need to add sum to sum then we need to write like int sum = sum + carry; otherwise I feel the 0 is redundant. Your thoughts on this please!!!
@nikoo28
@nikoo28 10 ай бұрын
0 + carry just seems familiar with what we are doing in the future. Makes your code more readable.
@naveenkumarkota2090
@naveenkumarkota2090 10 ай бұрын
understood thanks !
@Labib32
@Labib32 5 ай бұрын
Thanks you sir....
@RutwikKumbhar
@RutwikKumbhar Жыл бұрын
In the above case missing some test cases when both linked sizes are not the same that time above code will not work. Other your explanation is superb. add this code and sum variable will be global while(l2 != null){ sum = 0 + carry; sum += l2.val; l2 = l2.next; carry = sum / 10; sum = sum % 10; ptr.next = new ListNode(sum); ptr = ptr.next; } while(l1 != null){ sum = 0 + carry; sum += l1.val; l1 = l1.next; carry = sum / 10; sum = sum % 10; ptr.next = new ListNode(sum); ptr = ptr.next; }
@nikoo28
@nikoo28 Жыл бұрын
It will work, did you check the code available on Github?
@mennahassan5828
@mennahassan5828 Жыл бұрын
thank u so much.your explanation is quite simple
@nikoo28
@nikoo28 Жыл бұрын
Happy to help
@mennahassan5828
@mennahassan5828 Жыл бұрын
@@nikoo28 but why not all medium questions in leetcode have videos.i suggest to increase number of medium questions
@nikoo28
@nikoo28 Жыл бұрын
@@mennahassan5828 It takes time to create quality content. I take almost a total of 10 hours to create one video with proper thumbnails, companies that it has been asked in, github links, thumbnails, editing etc. etc.. I also try to upload the content in full 4K quality for best viewing experience. Nonetheless, stay tuned...I am constantly trying to cover more and more important questions as time permits :)
@mennahassan5828
@mennahassan5828 Жыл бұрын
@@nikoo28 thank you so much.appreciated
@thisisaniketup
@thisisaniketup Жыл бұрын
How will this eliminate the problem of test cases of very large numbers?
@nikoo28
@nikoo28 11 ай бұрын
because you are using linked lists. Each node will have only 1 digits. So your number can have 1000 digits, and still you will be able to add
@satishagrawal5015
@satishagrawal5015 Жыл бұрын
nicely explained ,thank you
@nikoo28
@nikoo28 Жыл бұрын
Glad it was helpful!
@kidoo1567
@kidoo1567 Жыл бұрын
Always we search for nikil's leetcode solution ❤😅
@nikoo28
@nikoo28 Жыл бұрын
wow...I am grateful for that.
@alejandrogonzalezalmazan3432
@alejandrogonzalezalmazan3432 Жыл бұрын
Wouldn’t the first element in your solution node (zero) affect the final result? I didn’t get that
@nikoo28
@nikoo28 Жыл бұрын
In the provided code, the initial node with value 0 is actually a placeholder node. This is a common technique used in linked list problems to simplify the code and avoid special cases at the beginning of the list. It doesn't affect the final result of the summation. As the code processes the linked lists l1 and l2, the actual result is constructed starting from the node after the placeholder node. This is why, throughout the loop, new nodes are added after the placeholder node (since the code initializes ptr as result). The code returns result.next, which is the node immediately after the placeholder node. This is the correct starting point for the final result linked list.
@chethananderson3446
@chethananderson3446 28 күн бұрын
Amazing
@YeaminiNawal
@YeaminiNawal Жыл бұрын
it was really helpful. thank you. just wanted to say that if the audio was clear it would have been more convenient, as ur voice feels like an echo. overall thanks
@reyazahmed4855
@reyazahmed4855 2 жыл бұрын
Yet another dsa channel. Same story different actor
@satyamrajrawat5947
@satyamrajrawat5947 3 ай бұрын
Why are you here then 🤡 ? Go do DSA on your own
@suneosama939
@suneosama939 Жыл бұрын
Hey bro i have a quesiton, why result next have the sum of ptr have add? I don't understand and question 2 is why you return head next instead of ptr next? Hmm maybe ptr next just a single node and result next all the node that ptr next have? Why result have all the sum that ptr next add, hmmm can you explain
@nikoo28
@nikoo28 Жыл бұрын
can you explain your question with an example? I am unable to follow your doubt.
@suneosama939
@suneosama939 Жыл бұрын
Its mean brother you're so good, understand that brother 😂😆
@boubacarboulkassoum2804
@boubacarboulkassoum2804 Жыл бұрын
Amazing bro keep going!! Love what you're doing. How can I contact you pls?
@nikoo28
@nikoo28 Жыл бұрын
You can contact me via e-mail. It is available in the "About" section of my channel :)
@worthyvibes9989
@worthyvibes9989 8 ай бұрын
In problem values are 5 and 2 are giving for sum then ? carry = sum /10; -> 7/10 = 0.7; why sum is saving -> sum %10 ?? any one know ?
@DataWorld1199
@DataWorld1199 Жыл бұрын
This is a question for this problem. if l1 has more nodes than l2, then l2 next node is null. if number+null, we will get null? We should consider this situation. is it?
@nikoo28
@nikoo28 Жыл бұрын
check the problem constraints. You may need to modify your code accordingly :)
@Novakid04
@Novakid04 Жыл бұрын
I dont understand why when we return result.next we get something iw we didnt add anything in it.
@nikoo28
@nikoo28 Жыл бұрын
result.next has the complete linked list
@Novakid04
@Novakid04 Жыл бұрын
@@nikoo28 Thank you, I figured it out after some time.
@suneosama939
@suneosama939 Жыл бұрын
Thank you
@anubhav.codess
@anubhav.codess Жыл бұрын
if(carry>0) { ptr.next=new Node(carry); } what if the value of carry is 2 or 3 or 4 ?
@nikoo28
@nikoo28 Жыл бұрын
When you add 2 digits, can the value of carry ever be more than 1 ??
@krsvlog1742
@krsvlog1742 10 ай бұрын
code didn't worked on leetcode man what to do ?
@nikoo28
@nikoo28 10 ай бұрын
that is not possible. try the code available on github (link in description)
@winningPages
@winningPages Жыл бұрын
Worth the time
@nikoo28
@nikoo28 Жыл бұрын
appreciate your feedback
@81_monish_roy74
@81_monish_roy74 3 ай бұрын
thanks brother please continue helping us like this. we need a teacher cum mentor like u.💓
@BCEKaushikKashyap
@BCEKaushikKashyap Жыл бұрын
thanku so much
@satyamrajrawat5947
@satyamrajrawat5947 3 ай бұрын
Great
@laxmanpatelpatel4657
@laxmanpatelpatel4657 Жыл бұрын
can you help do this type of question solution
@nikoo28
@nikoo28 Жыл бұрын
can you please elaborate?
@DreamQuestDiaries001
@DreamQuestDiaries001 11 ай бұрын
Which language has he used?
@nikoo28
@nikoo28 11 ай бұрын
JAVA
@pankajyadav-en7tb
@pankajyadav-en7tb Жыл бұрын
Wow!!.
@bibekanandapattanaik4293
@bibekanandapattanaik4293 Жыл бұрын
Thanks
@usmanrangrez-cd7zj
@usmanrangrez-cd7zj Жыл бұрын
class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *ans = new ListNode(0); ListNode *curr=ans; ListNode *p=l1; ListNode *q=l2; int carry = 0; while(p!=NULL || q!=NULL){ int firstData = (p!=NULL)?p->val:0; int secondData = (q!=NULL)?q->val:0; int sum=firstData+secondData+carry; carry=sum/10; int toPut = sum%10; curr->next = new ListNode(toPut); curr=curr->next; if(p!=NULL) p = p->next; if(q!=NULL) q = q->next; } //end pe agar carry milega 1 if(carry>0){ curr->next = new ListNode(carry); } return ans->next; } }; Different Approach
@hemanthreddy2
@hemanthreddy2 5 ай бұрын
Bro please take backtracking problems, they making me to leave whole DSA
@honey-xr5kp
@honey-xr5kp 9 ай бұрын
I must be doing something wrong, but all my test cases come back as wrong with this code.
@honey-xr5kp
@honey-xr5kp 9 ай бұрын
It adds an extra 0 to the beginning of the list each time. I'm not sure how or why?
@nikoo28
@nikoo28 9 ай бұрын
Did you have a look at the code given in video description?
@arijitmalakar6911
@arijitmalakar6911 7 ай бұрын
This solution is giving "time limit exceeded" error n leetcode...
@shubhamkishordevghare5826
@shubhamkishordevghare5826 4 ай бұрын
🎉
@sumitmalpure944
@sumitmalpure944 2 ай бұрын
I think result always dummy.next and when comes to travel to end use two pointers
@nikoo28
@nikoo28 2 ай бұрын
that will always work wonderfully for you
@chuckynorris616
@chuckynorris616 Жыл бұрын
praise lord ganesh
@nikoo28
@nikoo28 Жыл бұрын
🙏
@code-Dude
@code-Dude 10 ай бұрын
can someone help me /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { l1 = revesrseLL(l1); l2 = revesrseLL(l2); ListNode head = new ListNode(0); ListNode temp = head; int carry = 0; while( l1 != null || l2 != null ){ int sum = carry; if(l1 != null){ sum = sum + l1.val; l1 = l1.next; } if(l2 != null){ sum = sum + l2.val; l2 = l2.next; } //check carry and sum carry = sum / 10; sum = sum % 10; temp.next = new ListNode(sum); temp = temp.next; sum = 0; } if(carry==1){ temp.next = new ListNode(1); } return head.next; } public ListNode revesrseLL(ListNode head) { ListNode pre = null; ListNode cur = head; ListNode nxt; while (cur != null) { nxt = cur.next; cur.next = pre; pre = cur; cur = nxt; } return pre; } }
@JP-programming
@JP-programming Жыл бұрын
thanks for the great explanation.
L5. Add 2 numbers in LinkedList | Dummy Node Approach
14:48
take U forward
Рет қаралды 136 М.
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН
«Жат бауыр» телехикаясы І 26-бөлім
52:18
Qazaqstan TV / Қазақстан Ұлттық Арнасы
Рет қаралды 434 М.
Add Two Numbers - Leetcode 2 - Python
9:33
NeetCode
Рет қаралды 275 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 170 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 863 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 508 М.