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 contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0] Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]
Constraints:
- The number of nodes in each linked list is in the range
[1, 100]
. 0 <= Node.val <= 9
- It is guaranteed that the list represents a number that does not have leading zeros.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
dummy = ListNode(-1)
cur = dummy
while l1 or l2 or carry:
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
carry, val = divmod(s, 10)
cur.next = ListNode(val)
cur = cur.next
l1 = None if not l1 else l1.next
l2 = None if not l2 else l2.next
return dummy.next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (l1 != null || l2 != null || carry != 0) {
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = s / 10;
cur.next = new ListNode(s % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode* dummy = new ListNode(-1);
ListNode* cur = dummy;
while (l1 != NULL || l2 != NULL || carry != 0) {
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
carry = s / 10;
cur->next = new ListNode(s % 10);
cur = cur->next;
l1 = l1 == NULL ? NULL : l1->next;
l2 = l2 == NULL ? NULL : l2->next;
}
return dummy->next;
}
};
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function (l1, l2) {
let carry = 0;
const dummy = new ListNode(-1);
let cur = dummy;
while (l1 || l2 || carry) {
const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
carry = Math.floor(s / 10);
cur.next = new ListNode(s % 10);
cur = cur.next;
l1 = l1 ? l1.next : l1;
l2 = l2 ? l2.next : l2;
}
return dummy.next;
};
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
var carry = 0;
while (l1 != null || l2 != null || carry != 0)
{
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}