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.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4 * this.val = val; 5 * this.next = null; 6 * } 7 */ 8 /** 9 * @param {ListNode} l1 10 * @param {ListNode} l2 11 * @return {ListNode} 12 */ 13 var addTwoNumbers = function(l1, l2) { 14 // curry表示进位 15 var curry = 0; 16 var head = new ListNode(0); 17 var res = head; 18 19 while(l1 || l2) { 20 var val1 = l1 ? l1.val : 0; 21 var val2 = l2 ? l2.val : 0; 22 l1 = l1 ? l1.next : null; 23 l2 = l2 ? l2.next : null; 24 25 var val = val1 + val2 + curry; 26 curry = parseInt(val / 10); 27 val = val % 10; 28 29 var node = new ListNode(val); 30 head.next = node; 31 head = head.next; 32 } 33 if(curry > 0) { 34 var node = new ListNode(curry); 35 head.next = node; 36 } 37 return res.next; 38 };