package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

public class _2 {
    public static class Solution1 {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode result = new ListNode(0);
            ListNode tmp = result;
            int sum = 0;
            while (l1 != null || l2 != null) {
                sum /= 10;
                if (l1 != null) {
                    sum += l1.val;
                    l1 = l1.next;
                }
                if (l2 != null) {
                    sum += l2.val;
                    l2 = l2.next;
                }
                tmp.next = new ListNode(sum % 10);
                tmp = tmp.next;
            }
            if (sum / 10 == 1) {
                tmp.next = new ListNode(1);//this means there's a carry, so we add additional 1, e.g. [5] + [5] = [0, 1]
            }
            return result.val == 0 ? result.next : result;
        }
    }

}

@@ -132,16 +132,98 @@
 tion1 %7B%0A
+        /**%0A         * My completely original solution on 10/24/2021.%0A         */%0A
         
@@ -276,24 +276,51 @@
 tNode l2) %7B%0A
+            int carry = 0;%0A
             
@@ -328,22 +328,19 @@
 istNode 
+p
 re
-sult
  = new L
@@ -347,17 +347,18 @@
 istNode(
-0
+-1
 );%0A     
@@ -383,39 +383,11 @@
 p = 
+p
 re
-sult;%0A            int sum = 0
 ;%0A  
@@ -451,16 +451,18 @@
     
+int 
 sum 
-/
 = 
-1
 0;%0A 
@@ -694,32 +694,62 @@
               %7D%0A
+                sum += carry;%0A
                 
@@ -811,24 +811,58 @@
 = tmp.next;%0A
+                carry = sum / 10;%0A
             
@@ -883,21 +883,18 @@
 if (
-sum / 10 == 1
+carry != 0
 ) %7B%0A
@@ -937,159 +937,56 @@
 ode(
-1);//this means there's a carry, so we add additional 1, e.g. %5B5%5D + %5B5%5D = %5B0, 1%5D%0A            %7D%0A            return result.val == 0 ? result.next : resul
+carry);%0A            %7D%0A            return pre.nex
 t;%0A 

refactor 2