You are given two non-empty linked lists, where each list represents a non-negative integer.
The digits are stored in reverse order, and each node contains exactly one digit.
Your task is to add the two integers and return the result as a linked list, also stored in reverse order.
You may assume that neither number has leading zeros, except the number
Constraints:
The number of nodes in each linked list is in the range
Node.val
It is guaranteed that the list represents a number that does not have leading zeros.
One of the first ideas that might come to mind for adding the two numbers is to convert each linked list into a full integer, add them, and convert the result back. But these lists can represent very large numbers that exceed normal integer limits, making that approach unsafe and inefficient.
In this problem, each linked list stores its digits in reverse order, which means the least significant digit comes first. So, we can simulate addition digit by digit starting from the rightmost digit, just like doing arithmetic by hand. For example, when adding
You are given two non-empty linked lists, where each list represents a non-negative integer.
The digits are stored in reverse order, and each node contains exactly one digit.
Your task is to add the two integers and return the result as a linked list, also stored in reverse order.
You may assume that neither number has leading zeros, except the number
Constraints:
The number of nodes in each linked list is in the range
Node.val
It is guaranteed that the list represents a number that does not have leading zeros.
One of the first ideas that might come to mind for adding the two numbers is to convert each linked list into a full integer, add them, and convert the result back. But these lists can represent very large numbers that exceed normal integer limits, making that approach unsafe and inefficient.
In this problem, each linked list stores its digits in reverse order, which means the least significant digit comes first. So, we can simulate addition digit by digit starting from the rightmost digit, just like doing arithmetic by hand. For example, when adding