0

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

The linked list seems to be overwriting the nodes as far as I can tell. I did find the answer to this problem on GeeksforGeeks but I wanted help with figuring out what is wrong with my code. I also know my code is not the best in regards to optimization but any halp is accepted. Thanks!

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
    class Solution {
        //ListNode head;
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int x = 0;
            int y = 0;
            int z = 1;
    
            while(l1 != null){
                x += z*l1.val;
                z*=10;
                l1 = l1.next;
            }
          
            z = 1;
            while(l2 != null){
                y += z*l2.val;
                z*=10;
                l2 = l2.next;
            }
    
            int sum = x + y;
            ListNode node = new ListNode(0);
            
            while(sum > 0){
                int digit = sum % 10;
                ListNode n = new ListNode(digit);
                while(node.next != null){
                    node = node.next;
                }
                node.next = n;
                
                sum = sum / 10;
    
                
            }      
            return node;
    
        }
    }

LeetCode Question

1
  • how to traverse this? Commented Aug 2, 2022 at 10:48

4 Answers 4

2

I like your solution but you have bit incomplete logic.

class Solution {
    //ListNode head;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x = 0;
        int y = 0;
        int z = 1;

        while(l1 != null){
            x += z*l1.val;
            z*=10;
            l1 = l1.next;
        }

        z = 1;
        while(l2 != null){
            y += z*l2.val;
            z*=10;
            l2 = l2.next;
        }

        int sum = x + y;
        if (sum == 0) {
            return new ListNode(0);
        }

        ListNode node = null, head = null;
        while(sum > 0){
            int digit = sum % 10;
            ListNode n = new ListNode(digit);
            if (node == null) {
                head = node = n;
            } else {
                node.next = n;
                node = node.next;
            }
            
            sum = sum / 10;
        }

        return head;
    }
}

I just changed one or two things after int sum = x + y;

Sign up to request clarification or add additional context in comments.

2 Comments

But I guess this problem requires big integer arithmetics.
your solution is pretty impressive. i just wanted to know that how long did it take you to solve it? @alibek
0

This is the solution in kotlin Language.

class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
    
    var head1=l1
    var head2=l2

    var result:ListNode?=null
    var temp:ListNode?=null

    var carry=0

    while(head1 != null || head2 != null)
    {
        var sum=carry

       if(head1 != null)
       {

         sum +=  head1.`val`
         head1=head1.next

       }

       if(head2 != null)
       {
          sum +=head2.`val`
          head2= head2.next

       }
         
        val node= ListNode(sum%10)

        carry=sum/10

        if(temp== null)
        {

             result=node
             temp=result


        }

        else{ temp.next=node
        temp=temp.next}
       
    }

    if(carry>0){

        temp!!.next=ListNode(carry)
    }

return result
}
}

Comments

0

The solution in Java from my leetcode submission. Beats 100% of other solutions in runtime, and beats 97.3% of solutions in memory. It uses recursion of course.

 /* Definition for singly-linked list.
 public class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }*/
 
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int sum, carry;
        sum = l1.val + l2.val;
        carry = 0;
        if (sum >= 10){
            sum = sum - 10;
            carry = 1;
        }
        l1.val = sum;
        if(carry == 1){
            if(l1.next != null) {
                l1.next.val++;
            }
            else {
                l1.next = new ListNode(carry);
            } 
        }
        if(l1.next != null && l2.next!= null) addTwoNumbers(l1.next, l2.next);
        else if (l1.next!= null && l2.next == null) addTwoNumbers(l1.next, new ListNode(0));
        else if (l2.next != null && l1.next == null) {
            l1.next = new ListNode(0);
            addTwoNumbers(l1.next, l2.next);
        }
        return l1;
    }
}

Comments

0

The solution in PHP from my leetcode submission. Runtime 24ms,Memory 19.16MB.

class ListNode {
        public $val;
        public $next;
    
        public function __construct($val = 0, $next = null) {
            $this->val = $val;
            $this->next = $next;
        }
    }
    
    function addTwoNumbers($l1, $l2) {
        $result = new ListNode(0);
        $current = $result;
        $carry = 0;
    
        while ($l1 != null || $l2 != null) {
            $val1 = ($l1 != null) ? $l1->val : 0;
            $val2 = ($l2 != null) ? $l2->val : 0;
            $sum = $val1 + $val2 + $carry;
    
            $carry = intdiv($sum, 10);
            $current->next = new ListNode($sum % 10);
            $current = $current->next;
    
            if ($l1 != null) {
                $l1 = $l1->next;
            }
            if ($l2 != null) {
                $l2 = $l2->next;
            }
        }
    
        if ($carry > 0) {
            $current->next = new ListNode($carry);
        }
    
        return $result->next;
    }

'

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.