Simple Java Solution


  • 0
        public class Solution {
            public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                int carry = 0;
                ListNode dummy = new ListNode(0);
                ListNode c = dummy;
                while(l1 != null && l2 != null){
                    int gross = l1.val + l2.val + carry ;
                    int net = gross % 10;
                    carry = gross / 10;
                    l1 = l1.next;
                    l2 = l2.next;
                    c.next = new ListNode(net);
                    c = c.next;
                }
                while(l1 !=null){
                    int gross = l1.val + carry ;
                    int net = gross % 10;
                    carry = gross / 10;
                    l1 = l1.next;
                    c.next = new ListNode(net);
                    c = c.next;
                }
                while(l2 !=null){
                    int gross = l2.val + carry ;
                    int net = gross % 10;
                    carry = gross / 10;
                    l2 = l2.next;
                    c.next = new ListNode(net);
                    c = c.next;            
                }
                if(carry == 1)  c.next = new ListNode(1);
                return dummy.next;
            }
        }

Log in to reply
 

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