Understooooooooooooooood? . Instagram(connect if you want to know how a SDE's normal life is): instagram.com/striver_79/ . . If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
@elements86303 жыл бұрын
understoood thanks
@harshitrathi30773 жыл бұрын
Yea
@amishasahu15862 жыл бұрын
Understooooooooooooooooooood!!
@sunnykumarpal96792 жыл бұрын
Thank you bhaiya for making such videos . which ever topic i feel hard i try to find your videos. Your videos are really fabulous . Please be making such videos.
@ankitr29693 жыл бұрын
that's the tip, don't let interviewer know that u have already solved the problem. 😂
@Jaydeeeeeeep Жыл бұрын
What if they saw my leetcode profile?😂
@sanskarsinghiit Жыл бұрын
😂😂
@lohithreddy46282 ай бұрын
thats a greta tbh😂😂
@GeekyBaller4 жыл бұрын
I was asked this question in my interview with Amazon. Great work, Raj bhaiya!
@shivamverma94474 жыл бұрын
Hi, can you help me
@dutt_23023 жыл бұрын
this problem can be solved without any create new node. O(1) - space complexity
@trmpsanjay3 жыл бұрын
@@dutt_2302 yes by creating two pointer head and tail method
@muktadirkhan66743 жыл бұрын
@@dutt_2302 You would have to create a single node if the carry is there at the last node. And btw the solution Striver explained is also O(1). The space isnt counted for the variables we need to return.
@muktadirkhan66743 жыл бұрын
@@trmpsanjay Its a singly linked list, what are you going to achieve by a tail pointer?
@avishekroy16423 жыл бұрын
your way of teaching is very simple, and easy to understand
@settyruthvik62363 жыл бұрын
#code in python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def addTwoNumbers(self, A: Optional[ListNode], B: Optional[ListNode]) -> Optional[ListNode]: l3=ListNode(0) head=l3 carry=0 while A or B or carry: if A: carry += A.val A=A.next if B: carry += B.val B=B.next l3.val=carry%10 carry=carry//10 if A or B or carry: l3.next=ListNode(0) l3=l3.next return head
@mrinmoyaus7614 жыл бұрын
Great placement series is hitting bro just watching everytime i open youtube.
@abhay9994 Жыл бұрын
I wanted to send you a heartfelt thank you for your tireless dedication to teaching DSA for free. Your selflessness and passion for helping students with their job interview preparation have made a significant impact on my life and countless others. I am incredibly grateful for the knowledge and confidence you have imparted to us. Thank you for everything you do!❣✨✨
@tanishq2766 Жыл бұрын
I solved this without any extra space, making the additions in the node itself, was a bit complicated but yet it worked !
@avadheshsingh42553 жыл бұрын
You are amazing best programming teacher on youtube keep it up brother
@DeepakGupta-zz1vf4 жыл бұрын
we can also do like while(l1!=null || l2!=null) and after loop we can check if carry>0 then make a node else dont make
@muktadirkhan66743 жыл бұрын
Yeah saw this approach on the LeetCode solution.
@prabaljain58873 жыл бұрын
i was solving this question on leetcode and the logic hit me in just 2mins but i wasn't able to code it
@hrishitsarkar63882 жыл бұрын
Striver You are GREAT !!!!! Love from West Bengal
@NagaVijayKumar4 жыл бұрын
Thank you bro.. For uploading multiple videos in a single day..
@hemanthvarmas4 жыл бұрын
Nicely put explanation (as always) for "how to add two numbers given as Linked Lists". Thanks bro.
@dhruvrajput51944 жыл бұрын
Finally a question i figured out myself😂
@reallydarkmatter3 жыл бұрын
You are really amazing at explaining every concept clearly
@Metalrave2 жыл бұрын
I was able to do this in O(1) space as well with some optimisation (adding the sum values in the given listnodes only). Wouldn't that be a better answer to give to an interviewer?
@aakashgupta72112 жыл бұрын
We should try not to make any changes to the input provided.
@GameZone-yd8kr2 жыл бұрын
That's My Nigg@ ! I did that too ! Kinda Noob Solution but u get the 💡 : class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *ans = l1,*prev; int carry = 0; while(l1 && l2){ int sum = l1->val+l2->val+carry; if(sum > 9){ carry = 1; l1->val = sum-10; } else{ carry = 0; l1->val = sum; } prev = l1; l1 = l1->next; l2 = l2->next; } if(carry){ prev->next = l1 ? l1 : l2; while(l1){ int sum = l1->val+carry; if(sum > 9){ carry = 1; l1->val = sum-10; } else{ carry = 0; l1->val = sum; } prev = l1; l1 = l1->next; } while(l2){ int sum = l2->val+carry; if(sum > 9){ carry = 1; l2->val = sum-10; } else{ carry = 0; l2->val = sum; } prev = l2; l2 = l2->next; } if(carry){ ListNode *node = new ListNode(carry); prev->next = node; } } else prev->next = l1 ? l1 : l2; return ans; } };
@tanishq2766 Жыл бұрын
oh alright@@aakashgupta7211
@vishalrane14397 ай бұрын
bhai ekdam simple karke bataya, Thanks bro
@gamingwithhemend9890 Жыл бұрын
Love your simple and clean explanation 💯💯📸
@techdemy40504 жыл бұрын
It was a really awesome explanation if you don't mind can you make a video on " k Queues in a single array " and " k Stack in a single array " when you START WITH STACK and QUEUE because I am not able to understand these two questions.
@aravinds6406 Жыл бұрын
Well Explained
@deepgajjar52752 жыл бұрын
we can optimize space complexity by first calculating the sum and then using old linked list nodes to create a sum linked list
@Raju_Sharma852 Жыл бұрын
Yeah, i did the same while solving this problem. But we should not temper the input files/lists untill and unless we are allowed to do so. Better ask the interviewer whether modification to the input files is allowed or not.
@ujjwalgupta5182 жыл бұрын
Now I have the clearity on this question...thx ❤️
@jatinkumar4410 Жыл бұрын
Nice and clear explanation. One question... Should we consider the space used by output list when calculating space complexity?
@vakhariyajay22242 жыл бұрын
Thank you very much. You are a genius.
@priyankarai70054 жыл бұрын
instead of creating a new list we can just put the sum of every corresponding nodes in the nodes of the lists itself. Space complexity becomes from O(n) to O(1)
@takeUforward4 жыл бұрын
No you cannot as it us said dont destroy the given lists.
@laxminarayanchoudhary9393 жыл бұрын
@@takeUforward Thank you clearing, Same O(1) space complexity came in my mind.
@shri92292 жыл бұрын
intresting point anyways
@kaichang8186Ай бұрын
understood, thanks for the detail explanation
@Karan-vq5vg3 жыл бұрын
Amazing video...cleared all the doubts
@ritikashishodia6753 жыл бұрын
Great bhaiya... U always inspiration..... I am stuck in that problem.... Thanks to give idea and approach I am appreciate and to u always
@SudhanshuKumar-lp1nr3 жыл бұрын
Why do we need the dummy node in the starting why can't directly start from first sum value?
@manishmotwani37444 жыл бұрын
That #striver sir for providing such a wonderful content. It helps me a lot!!
@willturner34403 жыл бұрын
I think your approach better but not most optimized one Brother what if the interviewer say add without reversing
@DeepakLalchandaniProfile5 ай бұрын
How do you add without reversing ??? 😅😅😅
@princepavan-ip1lg Жыл бұрын
Awesome explaination sir ji👏👏👏👏
@adarshurmaliya99013 жыл бұрын
Great Explanation 👍
@-ShangsitNath2 жыл бұрын
Can we optimise the space by modifying anyone of the linked list and returning the head of that linked list ??
@28_vijayadityarajr12 Жыл бұрын
lovely vedio
@ankitbhatt56302 жыл бұрын
Good Explanation!
@aryansinha18182 жыл бұрын
8:22 C++ Code
@amansingh.h716 Жыл бұрын
iS THIS CORRECT BRUTEFORCE WHAT I AM THINKING if we Store these 2 link list in string then reverse it then parse this in INTEGER and then sum it and then again store this in string and then reverse it , and at last iterate it create new nodes link them .
@nabilafzal29843 жыл бұрын
Awesome explanation ..thanks Bhaiya
@ashdeepsingh67852 жыл бұрын
can someone pls explain me why are we checking for carry == 0 and exiting if carry is 0. It is possible that we add two numbers like 2+3 it wont generate carry and yet the list can continue to have more numbers.
@sourabhchoudhary72893 жыл бұрын
if list are not given in rev oreder then brute can be to make int out of linked list then Add them both and make new linked list of ans integer.
@Nikhil-qd9up3 жыл бұрын
OPTIMAL - We can also do it inplace without taking a dummy linkedlist
@freshcontent37293 жыл бұрын
but then why did striver say that this is the only optimal solution do this problem ? is he wrong ? please help me i am confused
@Nikhil-qd9up3 жыл бұрын
@@freshcontent3729 no bro he is correct,what i said was doing it inplace
@freshcontent37293 жыл бұрын
@@Nikhil-qd9up but bro doing inplace is optimal right ? as it is way better . cause we aint using space.
@Nikhil-qd9up3 жыл бұрын
@@freshcontent3729 yes bro
@freshcontent37293 жыл бұрын
@@Nikhil-qd9up so in interview which one should i tell ?
@itsugo_2 жыл бұрын
Thank you!
@AKD45363 жыл бұрын
Amazing explanation
@GurpreetSingh-ps6kl3 жыл бұрын
thank you
@amanjot34922 жыл бұрын
Good solution
@shashankgsharma09014 ай бұрын
Understood!
@aditik22334 жыл бұрын
understood Thank you
@gautamsuthar41433 жыл бұрын
It took me time but I wrote the code exactly same as you did.
@freshcontent37293 жыл бұрын
bro but this can be done without extra space also right ? that is withought making a whole new linked list ( the sum one )
@wog4299 Жыл бұрын
@@freshcontent3729 on which lists will you stored l1 or l2 , and how will you find which is greater and if the sum exceeds the list then again you will have to add a new Node which I guess taking a new list is great instead of modifying the given list
@riaria5982 Жыл бұрын
sum += carry; carry=sum/10; Isn't it butcher the sum value? It should be: carry= sum/10 sum = sum+ carry;
@abhirajtyagi92444 жыл бұрын
Nice sir je👌👌
@cossMania Жыл бұрын
Thanks man
@bhagyashri77292 жыл бұрын
can we do it in single temp node and avoid having 2 dummy pointers?
@krishnavamsichinnapareddy Жыл бұрын
Understood 🔥
@exdee84462 жыл бұрын
Why are we checking if (carry == 1) for loop's termination?
@p0tat06272 жыл бұрын
cause consider an example two list l1, l2 having only single digits, 8 & 7. If u sum it up u get 15. U add 5 to the new_list, the carry 1 gets ignored if u consider only l1 and l2 cause in the next loop l1 & l2 both null, but u still have 1 as carry left. Thats why carry should also be considered for the loop going..
@PranathNaik3 жыл бұрын
nice explaination
@sakshamsrivastava62803 жыл бұрын
hands down !!
@praharshsingh20953 жыл бұрын
What is the significance of using dummy pointer ?
@ajvindersingh68353 жыл бұрын
JUST TO STORE THE ADDRESS OF FIRST NODE😉
@achinttrivedi46672 жыл бұрын
@@ajvindersingh6835 @take U forward but where we are storing the address of first node can you explain please? and why we are returning dummy's next ?
@kishanyadav-ob4xl Жыл бұрын
Thanks sir
@pranayghosh79123 жыл бұрын
bhaia what is the difference betweeen striver n take u forward did u stopped striver?
@RM-lb7xw4 жыл бұрын
Great video bro, keep it up 👌
@subhajeetchakraborty17912 жыл бұрын
understoooood
@indhumathi5846 Жыл бұрын
understood
@aakashSky-0 Жыл бұрын
There would be a memory leak in this for dummy. So make sure you delete it at the end before returning.
@aakashdabas94904 жыл бұрын
why u did ' || ' not ' && ' : why this, while(l1 != NULL || l2 != NULL || carry!=0 ) not while(l1 != NULL && l2 != NULL && carry!=0 ) We have to check untill each of the l1 becomes NULL. Please answer sir!
@coding64094 жыл бұрын
if you use an && then this code will not work if the linked lists are not equal, using an || takes consideration of that edge case
@pratikshadhole66942 жыл бұрын
awesome bhai
@elements86303 жыл бұрын
nice video.
@fatehpreetsingh14402 жыл бұрын
gem
@ankitnegi48022 жыл бұрын
instead of || there should be && in while loop condition ...please clear it am i right or not??
@neerajmahapatra52392 жыл бұрын
Actually && will be be for stopping the while loop. But in while loop we are writing condition that should move the while loop... If any one of them is satisfied means if either n1 is not null or n2 is not null or carry is not 0 we have to move... It's not like if all of them satisfy then we have to move... Just say it in English you will get to know either we have to use && or ||
@surajsah3361 Жыл бұрын
I have a doubt, when we are returning dummy.next why it is returning whole node and why not the single node of dummy.next.
@tejaspatel2212 Жыл бұрын
It's the inbuilt implementation of leetcode for evaluation. Here, in leetcode when you return dummy.next, it will return the whole node same goes for return head. Hope this helps :)
@avishekroy16423 жыл бұрын
great content
@pragyavyas1863 жыл бұрын
We can do it without using the extra space also.
@takeUforward3 жыл бұрын
That will be destroying the original list, which is not allowed, also since you returning the answer which is taking space, it is okay!
@syedyaseenabbasrizvi80724 жыл бұрын
Can you please tell the intuition behind this approach..?
@takeUforward4 жыл бұрын
Its a simple algo, why do you need intuition. Isn’t it straightforward to add them like that, simple maths stuff!
@syedyaseenabbasrizvi80724 жыл бұрын
@@takeUforward Alright got it..thanku
@rohankrishnani2 жыл бұрын
please solve it without using space one time, i tried and am stuck with one testcase
@freshcontent37293 жыл бұрын
bro but this can be done without extra space also right ? that is withought making a whole new linked list ( the sum one )
@karangoyal73864 жыл бұрын
Bhaiya I was dropper but not selected should I go for local college in my city
@neerajyadav2349 Жыл бұрын
Java Code class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry=0; ListNode dummy=new ListNode(0,null); ListNode temp=dummy; while(l1!=null || l2!=null || carry!=0){ int ival=l1!=null?l1.val:0; int jval=l2!=null?l2.val:0; int sum = ival+jval+carry; ListNode node=new ListNode(sum%10); carry=sum/10; temp.next=node; temp=temp.next; if(l1!=null){ l1=l1.next; } if(l2!=null){ l2=l2.next;} } return dummy.next; } }
@tekken19353 жыл бұрын
Striver Bhaiya, do you think that having such SDE sheets, interviewer might explicitly look at the questions in such SDE sheets and not repeat these particular questions?
@amansinghal4663 Жыл бұрын
exactly what i am thinking.... interviewers also know that nowadays everyone is doing these sde sheets so they will definitely not ask questions present in it
@surbhigupta57775 ай бұрын
US
@amanchandok6854 жыл бұрын
When do you plan to finish this series?
@gandhijainamgunvantkumar67832 жыл бұрын
Understooooooooooooooood
@yashas35673 жыл бұрын
can u make a video for the same question if head points to the most significant digit
@sudheerg91823 жыл бұрын
then you need to reverse both list and do the same as explained
@yashchandan73133 жыл бұрын
Thankyouu!
@rohitgpt72013 жыл бұрын
hmmmmmm
@yashchandan73133 жыл бұрын
@@rohitgpt7201 😂 😂
@om_1_24 жыл бұрын
Got it bro👍👍
@hy-ti1ic2 жыл бұрын
Wow 😍
@shouvikdatta68314 жыл бұрын
Thnx vai❤
@mohdkhaleeq74682 жыл бұрын
on take you forward website space complexity is written O(max(m,n)) and you said O(n) it is confusing
@aryansingh71012 жыл бұрын
O(max(m,n)) will return the maximun of size od n or m, so it can be O(n) or O(m)
@preetitripathi62802 жыл бұрын
This solution is giving wrong answer for some test cases.What should be other edge cases which should be taken into consideration?