Add Two Numbers


  • 0
    V

    @luckyever619 consider this example number 235, this number is stored in linked list nodes : 5->3->2->Null


  • 0
    B

    I can this problem can be solved by space complexity O(1)


  • 0
    K

    got confused by the example, used to think the result should be store in normal order... this question is really confusing


  • 0
    W

    what is the meaning of curr.next = new ListNode(sum % 10);?


  • 0
    W

    @wangyiman

    (sum % 10) is the digit only version of sum
    for example

    if sum = 15 then sum % 10 = 5 which is what we want (5 for current return digit and 1 for carry on next digit)


  • 0
    A

    I don't know Java so the answer is frustrating to see because I can't translate it into Ruby easily. Guess I need to learn Java....


  • 0
    W

    @WinterLi thanks,it is very kind of you.


  • 0
    L

    Graceful solution. Much beter than my thought which contains too much braces and lines.


  • 0
    V

    Below is another solution with recursive call:

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2)
    {
    if(l1==null)return l2;
    else if(l2==null) return l1;
    else
    {
    int val = l1.val+l2.val>=10?l1.val+l2.val-10:l1.val+l2.val;
    ListNode result = new ListNode(val);
    result.next = addTwoNumbers(l1.next,l2.next);
    if(l1.val+l2.val>=10)
    result.next = addTwoNumbers(result.next,new ListNode(1));
    return result;
    }
    }


  • 0
    N

    @isaacjumac . So, result contains the head of the returning linkedlist right? What I don't understand is How can result point to the very first element of the linked list? result = new ListNode(0). so, result.val = 0 and result.next = null. Would you please help me to clarify my mistake. thanks


  • -1
    W

    /**

    • Definition for singly-linked list.
    • function ListNode(val) {
    • this.val = val;
      
    • this.next = null;
      
    • }
      /
      /
      *
    • @param {ListNode} l1
    • @param {ListNode} l2
    • @return {ListNode}
      */
      var addTwoNumbers = function(l1, l2) {
      var p = l1;
      var q = l2;
      var carry = 0;
      var head = new ListNode(0);
      var result = head;
      while(p!==null || q!== null){
      var sum = 0;
      var x = 0,y = 0;
      if(p !== null) x = p.val;
      if(q!==null) y = q.val;
      sum = x + y + carry;
      carry = Math.floor(sum/10);
      result.next = new ListNode(sum%10);
      result = result.next;
      if(p!==null) p = p.next;
      if(q!==null) q = q.next;
      }
      if(carry > 0){
      result.next = new ListNode(carry);
      }
      return head.next;
      };
      //I can implementation in javascript.,and the Math.floor() is required.

  • -1
    L

    @wangyiman said in Add Two Numbers:

    Math.floor

    you need round number , not Math.floor function.

    function roundNumber(number){
        return number-(number%1);
    }
    

  • 0

    @liangminhua said in Add Two Numbers:

    @wangyiman said in Add Two Numbers:

    Math.floor

    you need round number , not Math.floor function.

    function roundNumber(number){
        return number-(number%1);
    }
    

    How do you think that's different from Math.floor?


  • 0
    J

    c++, rather than sum one digit by one, sum up and then generate the linked list.

    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    		int n1 = 0, n2 = 0, i = 0, j = 0;
    		while (l1){
    			n1 += l1->val*pow(10, i++);
    			l1 = l1->next;
    		}
    		while (l2){
    			n2 += l2->val*pow(10, j++);
    			l2 = l2->next;
    		}
    		int sum = n1 + n2;
    		ListNode* res = new ListNode(-1);
    		ListNode* cur = res;
    		while (sum){
    			cur->next = new ListNode(sum%10);
    			sum /= 10;
    			cur = cur->next;
    		}
    		return res->next;
    	}
    

  • 0
    N

    @jzhanggg you'll run into issues if the value of input or combined result is greater than max int which on a 64 bit machine is 9,223,372,036,854,775,807. (from google)


  • 0
    J

    @nujohn, thanks a lot for pointing out that. This might be the reason they came up the problem.


  • 0
    L

    @ManuelP
    Sorry,I make a mistake.


  • 0
    X

    double free or corruption (out): 0x00007fff120e9320 *** , what about this error? I didn't write a "delete" or "free".


  • 0
    C

    javascript solution

    var addTwoNumbers = function (l1, l2) {
        let newLinked = new ListNode(0),
            p = l1,
            q = l2,
            curr = newLinked,
            carry = 0;
            while(p!==null||q!==null){
                let x = p!==null?p.val:0,
                    y = q!==null?q.val:0,
                    sum = carry + x + y;
                carry = Math.floor(sum / 10);
                curr.next = new ListNode(sum % 10);
                curr = curr.next;
                p!==null?p=p.next:null;
                q!==null?q=q.next:null;
            }
            if(carry>0){
                curr.next = new ListNode(carry);
            }
            return newLinked.next;
    };
    

  • -1
    P

    Has anyone been able to successfully submit a solution in Go? Can you please share your solution? My solution works for me locally, but the same testcase throws a runtime error on leetcode.

    This is my solution:
    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
    return addTwoNumebersUtil(l1, l2, 0)
    }

    func addTwoNumebersUtil(l1 *ListNode, l2 *ListNode, carry int) *ListNode {
    if l1 == nil && l2 == nil {
    if carry == 0 {
    return nil
    } else {
    return &ListNode{Val : carry, Next : nil}
    }
    }

    var v1, v2 int
    
    if l1 == nil {
        l1 = &ListNode{Val : 0, Next : nil}
    }
    v1 = l1.Val
    
    if l2 == nil {
        l1 = &ListNode{Val : 0, Next : nil}
    }
    v2 = l2.Val
    
    s := v1 + v2 + carry
    
    n := addTwoNumebersUtil(l1.Next, l2.Next, s / 10)
    
    return &ListNode {Val : s % 10, Next : n}
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.