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

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

Комментарии • 113

  •  10 месяцев назад +10

    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 2 года назад +28

    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 Год назад +1

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

  • @RashidIqbal-i9i
    @RashidIqbal-i9i Год назад +3

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

  • @DG_Theniel
    @DG_Theniel 7 месяцев назад +1

    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

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1  2 года назад +35

    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 Год назад +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 Год назад +1

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

    • @shubhamkate7849
      @shubhamkate7849 Год назад +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 11 месяцев назад

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

  • @rakeshruler2524
    @rakeshruler2524 2 года назад +1

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

  • @viencong
    @viencong 2 года назад +3

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

  • @SleepyBaseball-ts7se
    @SleepyBaseball-ts7se Год назад

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

  • @MdineshKumar-gp9wg
    @MdineshKumar-gp9wg 2 года назад

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

  • @hemantdixit4058
    @hemantdixit4058 2 года назад +2

    so much clarity in your explanation, amazing

  • @honey6567
    @honey6567 2 года назад +1

    VERY GOOD EXPLANATION KEEP MORE MORE VIDEOS IN LEET CODE THANKS

  • @zeyadalbadawy2245
    @zeyadalbadawy2245 Год назад

    Alish , your explaination is unparallel !!

  • @KashafZia-d7p
    @KashafZia-d7p 26 дней назад

    it is such a nice and brief explanation

  • @imran3509
    @imran3509 Год назад

    Gajab bhai mast samjhaya

  • @alastairhewitt380
    @alastairhewitt380 2 года назад +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 🙏🙏🙏

  • @negi-ji
    @negi-ji 2 года назад

    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🙏

  • @nakuldeshmukh7901
    @nakuldeshmukh7901 2 года назад +1

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

  • @abhi_jit_tripathy
    @abhi_jit_tripathy Год назад

    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........

  • @darshanpancholi8746
    @darshanpancholi8746 Год назад

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

  • @namrathas5804
    @namrathas5804 2 года назад +2

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

  • @himanshugupta3699
    @himanshugupta3699 2 года назад +2

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

  • @TeachingCodes
    @TeachingCodes Год назад

    Nice Explanation, Keep it up

  • @mayankjaiswal568
    @mayankjaiswal568 4 месяца назад

    Whats d point of doing that if we r taking new list
    It is advisable to do in same list without taking any extra space

  • @anshulvats1570
    @anshulvats1570 2 года назад

    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

  • @sreeharsha6383
    @sreeharsha6383 2 года назад +1

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

  • @_VISHALKUMAR-du2nu
    @_VISHALKUMAR-du2nu Год назад

    superb teaching skill

  • @Modern_Monk8926
    @Modern_Monk8926 4 месяца назад

    You know what you just gained one more subscriber😊😊 by the way thanks for the explanation

  • @karanpanchal5902
    @karanpanchal5902 2 года назад +2

    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?

  • @AbhiWarrior122
    @AbhiWarrior122 Год назад

    nice explaination Didi it really helps

  • @Khabib901
    @Khabib901 Год назад

    Great Explanation

  • @muhsinmansooriee_0017
    @muhsinmansooriee_0017 2 года назад

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

  • @Soumyajit_619
    @Soumyajit_619 11 месяцев назад

    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...

  • @PranavKamathe
    @PranavKamathe 6 месяцев назад +1

    Thank you Didi...!!✨

  • @menapatigowrishankar6236
    @menapatigowrishankar6236 Год назад

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

  • @anikmahmud1748
    @anikmahmud1748 2 года назад +1

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

  • @VivekPundir-r8e
    @VivekPundir-r8e 2 месяца назад

    Mam mera output wrong aa rha hai

  • @syedmunawar9809
    @syedmunawar9809 2 года назад

    I am begining to code and your video is helpful

  • @HARIHARAKRA
    @HARIHARAKRA 2 года назад

    Your explaination is soothing..keep going mam

  • @tarushidubey2973
    @tarushidubey2973 Год назад

    very well explained di

  • @tamzeedahmed1536
    @tamzeedahmed1536 Год назад

    Great explanation!!

  • @rts6649
    @rts6649 2 года назад +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 2 года назад

      same here - compilation error in line where we creating linklist

  • @sharadkumar6658
    @sharadkumar6658 2 года назад

    Nice mam. It was really good.😇😇

  • @cse0798
    @cse0798 2 года назад

    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

  • @holymakia
    @holymakia 2 года назад

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

  • @adyamansingh2983
    @adyamansingh2983 Год назад

    amazing explaination

  • @17.shubhamashish27
    @17.shubhamashish27 9 месяцев назад

    nice explaination

  • @PRITYKUMARI-zn5ro
    @PRITYKUMARI-zn5ro 2 года назад +1

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

  • @mukulyadav655
    @mukulyadav655 Год назад

    your codes are really helpful
    thanks

  • @jaydhanwalkar9665
    @jaydhanwalkar9665 Год назад

    what is time and space complexity of this code?

  • @Basantkumarr
    @Basantkumarr Год назад

    Amazing explanation 😊 please make more videos

  • @KartikSharma-yi4vg
    @KartikSharma-yi4vg Год назад

    Very Good Video

  • @Coding-Just
    @Coding-Just Год назад

    Thanks a lot 👍👍👍

  • @_Deepak__Verma
    @_Deepak__Verma 10 месяцев назад

    do it with recurrsion method because that is tricky

  • @sachinkapri4240
    @sachinkapri4240 6 месяцев назад

    Last test case still not working!

  • @omrajgure4553
    @omrajgure4553 2 года назад

    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 :)

  • @hameedmulani21
    @hameedmulani21 2 года назад +1

    Best explanation

  • @abhishekthakur7578
    @abhishekthakur7578 Год назад

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

  • @KaushalSharma-yo2yu
    @KaushalSharma-yo2yu Год назад

    perfect example of beauty with brain

  • @sayankarmakar13
    @sayankarmakar13 10 месяцев назад

    Appreciated ❤

  • @luckylam1181
    @luckylam1181 Год назад

    Thanks for the great explanation!

  • @bijoychandraroy
    @bijoychandraroy День назад

    Thanks

  • @oiabhii
    @oiabhii Год назад

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

  • @projectsdb4034
    @projectsdb4034 2 года назад

    Thanks Great Logic

  • @vitaminprotein2217
    @vitaminprotein2217 2 года назад

    A hardstuck will always understand from you :D

  • @krushnachandrajena6297
    @krushnachandrajena6297 9 месяцев назад

    Nice video,can i get the code?

  • @adarshkumargupta.
    @adarshkumargupta. 2 года назад

    vey nice explanation

  • @mikedelta658
    @mikedelta658 2 года назад

    Thanks Dear!

  • @santoshbhupati3579
    @santoshbhupati3579 Год назад

    Thank you

  • @pratikbhange4780
    @pratikbhange4780 2 года назад

    Keep it up!!

  • @34adnanali26
    @34adnanali26 2 месяца назад

    great

  • @newrolex8918
    @newrolex8918 2 года назад

    your voice is so sweet and melodious

  • @sumitftr
    @sumitftr Год назад +1

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

  • @davidl6797
    @davidl6797 2 года назад

    Thank you so much! Good explanation :)

  • @skp6753
    @skp6753 Год назад

    clear!!

  • @Rockers320
    @Rockers320 10 дней назад

    tq tq akka

  • @muhammadirahmonov417
    @muhammadirahmonov417 2 года назад +1

    good luck

  • @__ankush_kushwaha
    @__ankush_kushwaha 2 года назад

    mind blowing

  • @machj2592
    @machj2592 2 года назад

    i love you for this

  • @vaibhavgupta4720
    @vaibhavgupta4720 2 года назад

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

  • @ayush-6041
    @ayush-6041 Год назад

    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. 😢😢

  • @Programming-for-all-u2l
    @Programming-for-all-u2l Год назад

    I need all problems and java

  • @ashb8552
    @ashb8552 2 года назад

    thx

  • @ritiknandanwar8198
    @ritiknandanwar8198 2 года назад

    Where are you placed currently? Nice vid though

  • @RohitKumar-uz3hr
    @RohitKumar-uz3hr Год назад

    A beautiful girl with a beautiful explanation🙂🙂

  • @prasanna9440
    @prasanna9440 2 года назад

    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 😊😊

    • @prachigupta7197
      @prachigupta7197 2 года назад

      can u explain why it shows tle with while?

  • @technubie
    @technubie 2 года назад

    I wish you code in java also 😢

  • @AmoghSarangdhar
    @AmoghSarangdhar 2 года назад

    u deserve a subscribe

  • @technubie
    @technubie 2 года назад

    So cute 🥰

  • @17.shubhamashish27
    @17.shubhamashish27 9 месяцев назад

    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
    };

  • @ajitpalsingh606
    @ajitpalsingh606 2 года назад

    243+564=807 not 708

  • @Piyush-wm2wj
    @Piyush-wm2wj 11 месяцев назад

    Sakal piche rakh kr bnaya jro

  • @SandeepSingh-nc1zm
    @SandeepSingh-nc1zm 2 года назад

    goodie

  • @spikemza6610
    @spikemza6610 8 месяцев назад +1

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

    • @fallen7
      @fallen7 4 месяца назад

      Comeon man that was a fine explanation she cant teach u by grabbing your fingers

  • @ratneshtiwariaxiscollegesc7144
    @ratneshtiwariaxiscollegesc7144 7 месяцев назад

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

  • @amituknow2814
    @amituknow2814 Год назад

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

  • @amituknow2814
    @amituknow2814 Год назад

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

  • @projectsdb4034
    @projectsdb4034 2 года назад +4

    //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 Год назад

    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 2 года назад

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