Leetcode #2 - Add Two Numbers (Solution)

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

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

  • @expansivegymnast1020
    @expansivegymnast1020 3 года назад

    THANK YOU!!!!!!!!!!!!!!!!!!!!!!!

  • @tripathi5174
    @tripathi5174 4 года назад

    man you are o good at explaining this , please upload more .

  • @Abhishek44503
    @Abhishek44503 5 лет назад +3

    You explain it so well ! Please do more leetcode explanations

    • @algorithmsillustrator3313
      @algorithmsillustrator3313  5 лет назад +1

      Thank you for your kind words Abhishek! Will definitely try more. Have been busy with work so haven't gotten chance.

  • @rayprusia4753
    @rayprusia4753 5 лет назад +1

    You do a good job explaining how to solve a problem.

  • @Sky-nt1hy
    @Sky-nt1hy 4 года назад

    Thanks bro

  • @The-Entelechy
    @The-Entelechy 4 года назад

    just getting started with relearning linked lists. How does the created list solution get connected back to ret.next
    I can visualize how curr.next makes a new node and setting curr=curr.next shifts the pointer to the new node for the next iteration. Is it from a background function I don't see, or is it just how returns work and that's what I need to study. Thank you!

    • @arjunbharadwaj1820
      @arjunbharadwaj1820 4 года назад +1

      You just keep another dummy pointer to the beginning of the list, which I have called newList. The other pointer like tmp or current keeps on moving to the next and next while constructing the list, but since you have another pointer at the beginning of list, you just return newList.next;. Hope that helps.

  • @shahzadkarim7134
    @shahzadkarim7134 4 года назад

    for the carry, totalsum / 10 gives a non integer, should you not use int(totalsum/10)?

    • @arjunbharadwaj1820
      @arjunbharadwaj1820 4 года назад

      Yes, it should have been carry = totalSum // 10 , since // does integer division in python

  • @sushmam4404
    @sushmam4404 4 года назад +1

    clear explanation . subscribed:)

  • @mahsaz7
    @mahsaz7 5 лет назад +1

    Thank you for the explanation. Can you explain why newlist.next gives all the digits of the sum (708) rather than only the first digit of the sum which is 7. How come "next" gives the whole list of values?

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

    please do

  • @AA-jl1gp
    @AA-jl1gp 4 года назад

    Thank you!

  • @prashanthnaik7534
    @prashanthnaik7534 5 лет назад

    does this work when the li = [5] and l2 = [5] ? I was trying the same in JS, and it fails for this test case.. here is my code
    var tempList = new ListNode(0);
    var head = tempList;
    var sum = 0;
    var carry = 0;
    while (l1 || l2) {
    if (l1) {
    sum += l1.val; // 3
    l1 = l1.next; // 4 node
    }
    if (l2) {
    sum += l2.val; // 3+4=7
    l2 = l2.next; // 6 node
    }
    sum = sum + carry; // 7+0=7
    carry = Math.floor(sum/10); // 0
    head.next = new ListNode(sum % 10); // 7
    sum = 0;
    head = head.next; // 7
    }
    return tempList.next;

  • @ramnathan4236
    @ramnathan4236 5 лет назад

    thank you bro!!

  • @cocoarecords
    @cocoarecords 5 лет назад

    Thanks subbed

  • @tianhao7783
    @tianhao7783 5 лет назад

    nice try