public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ln1 = l1, ln2 = l2, head = null, node = null;
int carry = 0, remainder = 0, sum = 0;
head = node = new ListNode(0);
while(ln1 != null  ln2 != null  carry != 0) {
sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry;
carry = sum / 10;
remainder = sum % 10;
node = node.next = new ListNode(remainder);
ln1 = (ln1 != null ? ln1.next : null);
ln2 = (ln2 != null ? ln2.next : null);
}
return head.next;
}
}
4ms 11lines java solution



@swgrhck
I guess when u post this they haven't add so many test cases,
it doesn't pass with this one[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9] [1]
here is my solution
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dum = new ListNode(1), curt = null; int sum = 0, carry = 0; curt = dum; while (l1 != null  l2 != null  carry != 0) { sum = 0; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } sum += carry; carry = sum / 10; ListNode temp = new ListNode(sum % 10); curt.next = temp; curt = curt.next; } return dum.next; }

@Lara I do not see much difference with the best solution. Also, I test by the following code. The cost time is similar.
public class AddTwoNumber {public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dum = new ListNode(1), curt = null; int sum = 0, carry = 0; curt = dum; while (l1 != null  l2 != null  carry != 0) { sum = 0; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } sum += carry; carry = sum / 10; ListNode temp = new ListNode(sum % 10); curt.next = temp; curt = curt.next; } return dum.next; } public ListNode addTwoNumbers2(ListNode l1, ListNode l2) { ListNode ln1 = l1, ln2 = l2, head = null, node = null; int carry = 0, remainder = 0, sum = 0; head = node = new ListNode(0); while (ln1 != null  ln2 != null  carry != 0) { sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry; carry = sum / 10; remainder = sum % 10; node = node.next = new ListNode(remainder); ln1 = (ln1 != null ? ln1.next : null); ln2 = (ln2 != null ? ln2.next : null); } return head.next; } public static ListNode createNode(String sentence) { ListNode head = new ListNode(0); for (int i = 0; i < sentence.length(); i++) { char c = sentence.charAt(i); if (c >= '0' && c <= '9') { ListNode newNode = new ListNode(c  '0'); newNode.next = head.next; head.next = newNode; } } return head.next; } /** * [9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9] * [1] * * @param args */ public static void main(String[] args) { String s1 = "[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]"; String s2 ="[1]"; ListNode l1 = createNode(s1); ListNode l2 = createNode(s2); long startTime=System.nanoTime(); ListNode mergeL3 = new AddTwoNumber().addTwoNumbers(l1, l2); long endTime=System.nanoTime(); System.out.println("程序运行时间： "+(endTimestartTime)+"ns"); startTime=System.nanoTime(); ListNode mergeL4 = new AddTwoNumber().addTwoNumbers2(l1, l2); endTime=System.nanoTime(); System.out.println("程序运行时间： "+(endTimestartTime)+"ns"); }
}

@swgrhck Good solution. Could be simplified somewhat by making the first line in the method
ListNode ln1 = l1, ln2 = l2, head = new ListNode(0), node = head;
and then removing the third line
