Leetcode 2. Add Two Numbers | Add digits of two linked lists and return their sum | Linked List

Sdílet
Vložit
  • čas přidán 9. 03. 2022
  • Connect with me on LinkedIn : / alisha-parveen-80579850
    Check out our other playlists:
    Dynamic Programming:
    • Dynamic Programming
    Trees:
    • Trees
    Heaps and Maps:
    • Heaps and Maps
    Arrays and Maths:
    • Arrays and Maths
    Bit Manipulation:
    • Bit Manipulation
    Greedy Algorithms:
    • Greedy Algorithms
    Sorting and Searching:
    • Sorting and Searching
    Strings:
    • Strings
    Linked Lists:
    • Linked Lists
    Stack and Queues:
    • Stacks and Queues
    Two Pointers:
    • Two pointers
    Graphs, BFS, DFS:
    • Graphs, DFS, BFS
    Backtracking:
    • Backtracking
    Non- DSA playlists:
    Probability:
    • Probability
    SQL-Basic Join functions:
    • SQL - Basic JOIN Funct...
    SQL-Basic Aggregate functions:
    • SQL-Basic Aggregate Fu...

Komentáře • 109

  •  Před 4 měsíci +3

    You can avoid some code duplication by doing like this:
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

    ListNode l3 = new ListNode(-1);
    ListNode l3Head = l3;
    int carry = 0;
    while (l1 != null || l2 != null){
    int l1Val = 0;
    if (l1 != null) {
    l1Val = l1.val;
    l1 = l1.next;
    }

    int l2Val = 0;
    if (l2 != null){
    l2Val = l2.val;
    l2 = l2.next;
    }

    int value = l1Val + l2Val + carry;
    l3.next = new ListNode(value % 10);
    carry = value / 10;

    l3 = l3.next;
    }
    if (carry != 0){
    l3.next = new ListNode(carry);
    }
    return l3Head.next;
    }

  • @lifekool5838
    @lifekool5838 Před rokem +22

    in depth knowledge of how to solve a particular problem and how to take care of edge cases. Best explanation ever

  • @ParthG-vi4ml
    @ParthG-vi4ml Před 6 měsíci +1

    really 😃 breaking down the problem into smaller chunks & solving every chunk 1 by 1 is the best thing of this channel.

  • @nakuldeshmukh7901
    @nakuldeshmukh7901 Před rokem +1

    best explanation of this problem on yt. Very simple to understand .Kudos!

  • @rakeshruler2524
    @rakeshruler2524 Před rokem +1

    You explained it in student way thank you for the simple logic ! A lots of love

  • @user-mc7rp9un5o
    @user-mc7rp9un5o Před 9 měsíci +2

    I see many videos, but you explain very well. Best explanation and easy to understand.

  • @MdineshKumar-gp9wg
    @MdineshKumar-gp9wg Před rokem

    i started coding last few weeks.your viedoes was so helpful to me .thank you

  • @eimi_colony
    @eimi_colony Před měsícem

    been looking several videos of this problem. This is by far the best explaination video I have ever seen. Finally someone who can shove down information in my brain. Thank you

  • @kennyfully88
    @kennyfully88 Před 3 měsíci

    I never had to use linked lists before so when I came to this LeetCode problem I was freaking out! It took me about 5 hours to solve it! Your video explained it in a great way! Thank you very much!

  • @namrathas5804
    @namrathas5804 Před rokem +2

    Thank you so much Alisha, your code and explanation was so easy to understand.

  • @cse0798
    @cse0798 Před 2 lety

    thanku so much di I understand 100 percent of your explanation with clarity because you explain so good keep it up di and help all

  • @SleepyBaseball-ts7se
    @SleepyBaseball-ts7se Před 5 měsíci

    Ma'am, I'm working with java but I got complete logic of the code Hats off to you Ma'am

  • @honey6567
    @honey6567 Před rokem +1

    VERY GOOD EXPLANATION KEEP MORE MORE VIDEOS IN LEET CODE THANKS

  • @viencong
    @viencong Před rokem +3

    100 point for you. Love you very much for explain so eassy to understand

  • @syedmunawar9809
    @syedmunawar9809 Před 2 lety

    I am begining to code and your video is helpful

  • @PRITYKUMARI-zn5ro
    @PRITYKUMARI-zn5ro Před 2 lety +1

    your explanation is so smooth and clear.thanks.
    can you plz mention time complexity with your solution..it would be very useful.

  • @florianion4633
    @florianion4633 Před rokem

    omg so much work put into this channel. thx for help and great work

  • @darshanpancholi8746
    @darshanpancholi8746 Před 6 měsíci

    just loved the way you explained....❤❤

  • @oiabhii
    @oiabhii Před rokem

    Great explanation! please give time and space complexity too that would really help us all.

  • @HARIHARAKRA
    @HARIHARAKRA Před rokem

    Your explaination is soothing..keep going mam

  • @luckylam1181
    @luckylam1181 Před rokem

    Thanks for the great explanation!

  • @alastairhewitt380
    @alastairhewitt380 Před rokem +3

    One of the best explanations I've seen. However, what is still throwing me off with LeetCode's solution was that they had l3 = head rather than head = l3 as you have here and I have been hung up on it for a while. I would appreciate it a lot if you could please help me understand why the order of the assignment doesn't seem to matter 🙏🙏🙏

  • @davidl6797
    @davidl6797 Před rokem

    Thank you so much! Good explanation :)

  • @abhijittripathy9486
    @abhijittripathy9486 Před 11 měsíci

    thank you so much for this great explanation....I soled this problem in 3 different ways but some test cases didn't ran...When i saw ur explanation it was super easy...thanks a lot for this........

  • @zeyadalbadawy2245
    @zeyadalbadawy2245 Před 11 měsíci

    Alish , your explaination is unparallel !!

  • @hemantdixit4058
    @hemantdixit4058 Před 2 lety +1

    so much clarity in your explanation, amazing

  • @anikmahmud1748
    @anikmahmud1748 Před rokem +1

    carry on sister.your explanation is better than any other explanation❤❤❤❤❤

  • @TeachingCodes
    @TeachingCodes Před rokem

    Nice Explanation, Keep it up

  • @PranavKamathe
    @PranavKamathe Před 16 hodinami +1

    Thank you Didi...!!✨

  • @sreeharsha6383
    @sreeharsha6383 Před 2 lety

    but it is always recommended to do in constant space right ?

  • @himanshugupta3699
    @himanshugupta3699 Před rokem +1

    You have no idea how confused I was in this..Thankyou for making this video

  • @mukulyadav655
    @mukulyadav655 Před rokem

    your codes are really helpful
    thanks

  • @projectsdb4034
    @projectsdb4034 Před rokem

    Thanks Great Logic

  • @Khabib901
    @Khabib901 Před rokem

    Great Explanation

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1  Před 2 lety +33

    ListNode* l3 = new ListNode(0);
    int carry =0;
    ListNode* head = l3;
    while(l1 && l2)
    {
    int value = l1->val+l2->val+carry;
    carry = value/10;
    l3->next = new ListNode(value%10);
    l3 = l3->next;
    l1 = l1->next;
    l2 = l2->next;
    }

    while(l1)
    {
    int value = l1->val+carry;
    carry = value/10;
    l3->next = new ListNode(value%10);
    l3 = l3->next;
    l1 = l1->next;

    }

    while(l2)
    {
    int value = l2->val+carry;
    carry = value/10;
    l3->next = new ListNode(value%10);
    l3 = l3->next;
    l2 = l2->next;

    }

    if(carry)
    {
    l3 ->next = new ListNode(carry);
    }
    return head->next;

    • @saijeswanth6673
      @saijeswanth6673 Před rokem +2

      Is this Code is working in leetcode. It shows L3 statements at all cases getting error. L3 Statement is not a statement. Can anyone please help for this issue. Why it is coming

    • @shubhamkate7849
      @shubhamkate7849 Před 11 měsíci +1

      @@saijeswanth6673 if you working in java use ListNode l3 = new ListNode (0);

    • @shubhamkate7849
      @shubhamkate7849 Před 11 měsíci +1

      @@saijeswanth6673 class Solution {
      public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
      ListNode l3= new ListNode(0);
      ListNode newhead=l3;
      int carry=0;
      while(l1!=null && l2!=null){
      int result= l1.val+l2.val+carry;
      carry=result/10;
      l3.next= new ListNode(result%10);
      l3=l3.next;
      l1=l1.next;
      l2=l2.next;
      }
      while(l1!=null){
      int result= l1.val+carry;
      carry=result/10;
      l3.next= new ListNode(result%10);
      l3=l3.next;
      l1=l1.next;
      }
      while(l2!=null){
      int result= l2.val+carry;
      carry=result/10;
      l3.next= new ListNode(result%10);
      l3=l3.next;
      l2=l2.next;
      }
      if(carry!=0){
      l3.next= new ListNode(carry);
      }
      return newhead.next;
      }
      }

    • @ashishshetty4363
      @ashishshetty4363 Před 5 měsíci

      @@shubhamkate7849 can anyone explain why her code is not working

  • @sayankarmakar13
    @sayankarmakar13 Před 4 měsíci

    Appreciated ❤

  • @ArjunParmar04
    @ArjunParmar04 Před 6 měsíci

    explained very welll thanks...

  • @tamzeedahmed1536
    @tamzeedahmed1536 Před 7 měsíci

    Great explanation!!

  • @mikedelta658
    @mikedelta658 Před rokem

    Thanks Dear!

  • @menapatigowrishankar6236

    How do you gain this logic to solve this kind of probs, let me know please, any tips to solve..

  • @imran3509
    @imran3509 Před 8 měsíci

    Gajab bhai mast samjhaya

  • @Basantkumarr
    @Basantkumarr Před 9 měsíci

    Amazing explanation 😊 please make more videos

  • @AbhishekGupta-ie7sj
    @AbhishekGupta-ie7sj Před 7 měsíci

    nice explaination Didi it really helps

  • @pratikbhange4780
    @pratikbhange4780 Před 2 lety

    Keep it up!!

  • @Coding-Just
    @Coding-Just Před rokem

    Thanks a lot 👍👍👍

  • @ArjunParmar04
    @ArjunParmar04 Před 6 měsíci

    great solution....

  • @jaydhanwalkar9665
    @jaydhanwalkar9665 Před 10 měsíci

    what is time and space complexity of this code?

  • @sharadkumar6658
    @sharadkumar6658 Před rokem

    Nice mam. It was really good.😇😇

  • @17.shubhamashish27
    @17.shubhamashish27 Před 3 měsíci

    nice explaination

  • @muhsinmansooriee_0017
    @muhsinmansooriee_0017 Před 2 lety

    this is the first video on youtube that truly made me feel to subscribe. Thanks a lot.

  • @vatsvlogs6110
    @vatsvlogs6110 Před rokem +1

    Thanks 👍🏻

  • @hameedmulani21
    @hameedmulani21 Před 2 lety +1

    Best explanation

  • @tarushidubey2973
    @tarushidubey2973 Před rokem

    very well explained di

  • @_VISHALKUMAR-du2nu
    @_VISHALKUMAR-du2nu Před 5 měsíci

    superb teaching skill

  • @negi-ji
    @negi-ji Před rokem

    thankew thankew thankew ! itna ache se samjhane ke liye. i was stuck in this problem because of the corner cases. Baaki videos se kuch khaas samaj nahi aya but with this video i got it all in one go! replay bhi nahi karna pada and i wrote it in java. once again thanking you🫂... bhagwan aapko ek world tour gift kar de meri taraf se🙏

  • @adyamansingh2983
    @adyamansingh2983 Před 11 měsíci

    amazing explaination

  • @adarshkumargupta.
    @adarshkumargupta. Před rokem

    vey nice explanation

  • @skp6753
    @skp6753 Před 10 měsíci

    clear!!

  • @__ankush_kushwaha
    @__ankush_kushwaha Před rokem

    mind blowing

  • @santoshbhupati3579
    @santoshbhupati3579 Před rokem

    Thank you

  • @karanpanchal5902
    @karanpanchal5902 Před rokem +1

    I don't why know but i am getting this error as :
    error: not a statement
    l3->next=new ListNode(value % 10);
    ^
    can you help with this?

  • @vitaminprotein2217
    @vitaminprotein2217 Před rokem

    A hardstuck will always understand from you :D

  • @anshulvats1570
    @anshulvats1570 Před 2 lety

    Arey wah. Maine bilkul aisa hi code likha tha java mein bus ye extra while loops nhi likhey they (when length of lists are not same)
    hmmm...
    Very nice explanation @Alisha
    Keep growing

  • @Soumyajit_619
    @Soumyajit_619 Před 4 měsíci

    Can you tell me how did you get so good at this? It feels like you know every step as soon as you read the question, although I am new to coding I am completely lost here...

  • @omrajgure4553
    @omrajgure4553 Před rokem

    Your explanation and all are fantastic but I would like to give you a feedback about try keeping your mic slightly away...it irritates sometimes.
    Thank you :)

  • @machj2592
    @machj2592 Před rokem

    i love you for this

  • @KaushalSharma-yo2yu
    @KaushalSharma-yo2yu Před rokem

    perfect example of beauty with brain

  • @rts6649
    @rts6649 Před rokem +1

    Hey, can you just me how can I solve problem without getting errror. My solution is correct but keep saying error
    can u make video on that.

    • @priyanka4737
      @priyanka4737 Před rokem

      same here - compilation error in line where we creating linklist

  • @newrolex8918
    @newrolex8918 Před rokem

    your voice is so sweet and melodious

  • @KartikSharma-yi4vg
    @KartikSharma-yi4vg Před rokem

    Very Good Video

  • @ashb8552
    @ashb8552 Před rokem

    thx

  • @sachinkapri4240
    @sachinkapri4240 Před 10 dny

    Last test case still not working!

  • @_Deepak__Verma
    @_Deepak__Verma Před 4 měsíci

    do it with recurrsion method because that is tricky

  • @AmoghSarangdhar
    @AmoghSarangdhar Před rokem

    u deserve a subscribe

  • @abhishekthakur7578
    @abhishekthakur7578 Před rokem

    good explaination but please keep your speed slow we are just new in programming

  • @krushnachandrajena6297
    @krushnachandrajena6297 Před 3 měsíci

    Nice video,can i get the code?

  • @sumitftr
    @sumitftr Před rokem +1

    I didn't noticed the reverse word in the question and tried to solve this problem with wrong concept for three days

  • @technubie
    @technubie Před rokem

    So cute 🥰

  • @vaibhavgupta4720
    @vaibhavgupta4720 Před rokem

    Mam in this video you just look like's , just now woke up 😁😁

  • @infomania_by_ayush7901
    @infomania_by_ayush7901 Před 7 měsíci

    So today, I was trying to add the numbers by storing in string and then reversing the string and what not and finally I cleared 1565 test cases failed 3. 😢😢

  • @muhammadirahmonov417
    @muhammadirahmonov417 Před rokem +1

    good luck

  • @TeluguAbbai802
    @TeluguAbbai802 Před 6 měsíci

    I need all problems and java

  • @prasanna9440
    @prasanna9440 Před rokem

    The thing I've learnt is writing while(carry) exceeds time limit, so we write if(carry)..
    Thank you ma'am for this kind explanation 😊😊

  • @technubie
    @technubie Před rokem

    I wish you code in java also 😢

  • @SandeepSingh-nc1zm
    @SandeepSingh-nc1zm Před 2 lety

    goodie

  • @RohitKumar-uz3hr
    @RohitKumar-uz3hr Před rokem

    A beautiful girl with a beautiful explanation🙂🙂

  • @17.shubhamashish27
    @17.shubhamashish27 Před 3 měsíci

    nice way of teaching i've done some new watching your logic
    var addTwoNumbers = function (l1, l2) {
    let node = new ListNode(0)
    let curr = node;
    let carry = 0;
    let sum = 0;
    while (l1 || l2 || carry) {
    if (!l2) {
    l2 = new ListNode(0)
    }
    if (!l1) {
    l1 = new ListNode(0)
    }
    sum = l1.val + l2.val + carry
    carry = Math.floor(sum / 10)
    let r = sum % 10
    curr.next = new ListNode(r)
    curr = curr.next
    l1 = l1.next
    l2 = l2.next
    }
    // if(carry){
    // curr.next=new ListNode(carry)
    // }
    return node.next
    };

  • @ritiknandanwar8198
    @ritiknandanwar8198 Před 2 lety

    Where are you placed currently? Nice vid though

  • @ajitpalsingh606
    @ajitpalsingh606 Před rokem

    243+564=807 not 708

  • @Piyush-wm2wj
    @Piyush-wm2wj Před 5 měsíci

    Sakal piche rakh kr bnaya jro

  • @amituknow2814
    @amituknow2814 Před 11 měsíci

    video play karte ho to cute sa face dikha tumara, pyar ho gya :)

  • @amituknow2814
    @amituknow2814 Před 11 měsíci

    nhi samaj aya, hindi me samghati to acha hota..kisi aur ki video dekhta hu

  • @ratneshtiwariaxiscollegesc7144

    You think yourself genius , worst kind of explanation, please first learn to explain

  • @spikemza6610
    @spikemza6610 Před měsícem

    she's not explaining she's just practicing herself worse kind of explanation

  • @projectsdb4034
    @projectsdb4034 Před rokem +3

    //Java Code
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode l3 = new ListNode(0);
    ListNode head = new ListNode();
    head = l3;
    int carry = 0;
    while(l1!=null && l2!=null){
    int value = l1.val+l2.val+carry;
    carry = value/10;
    l3.next = new ListNode(value%10);
    l3 = l3.next;
    l2 = l2.next;
    l1 = l1.next;
    }
    while(l1!=null){
    int value = l1.val+carry;
    carry = value/10;
    l3.next = new ListNode(value%10);
    l3 = l3.next;
    l1 = l1.next;
    }
    while(l2!=null){
    int value = l2.val+carry;
    carry = value/10;
    l3.next = new ListNode(value%10);
    l3 = l3.next;
    l2 = l2.next;
    }
    if(carry>0){
    l3.next = new ListNode(carry);
    }
    return head.next;
    }
    }

  • @abhishek6575
    @abhishek6575 Před rokem

    leetcode easy sol by me
    class Solution {
    private:
    ListNode* reverse(ListNode* head) {

    ListNode* curr = head;
    ListNode* prev = NULL;
    ListNode* next = NULL;

    while(curr != NULL) {
    next = curr -> next;
    curr -> next = prev;
    prev = curr;
    curr = next;
    }
    return prev;
    }
    public:
    ListNode* addTwoNumbers(ListNode* lis1, ListNode* lis2) {
    ListNode*l1=reverse(lis1);
    ListNode*l2=reverse(lis2);
    ListNode*dummy=new ListNode();
    ListNode*temp=dummy;
    int carry=0;
    while(l1!=NULL||l2!=NULL||carry){
    int sum=0;
    sum=sum+carry;
    if(l1!=NULL){
    sum+=l1->val;
    l1=l1->next;}
    if(l2!=NULL){
    sum+=l2->val;
    l2=l2->next;}

    carry=sum/10;

    ListNode* node=new ListNode(sum%10);sum+=carry;
    temp->next=node;
    temp=temp->next;
    }temp=dummy;

    ListNode*curr=reverse(temp->next);
    return curr;
    }
    };

  • @hsg15
    @hsg15 Před rokem

    OP ma'am .. I'm getting u bit by bit every bit 🫡🔥