lc2.Add Two Numbers
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:
|
|
Example 2:
|
|
Example 3:
|
|
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.
Thinking
To understand the question, we need to specify:
2->4->3 stands for the number 342
5->6->4 stands for the number 465
So, the sum should be 807, which can be represented as 7->0->8
Then the process goes easily.
Firstly we add 2 and 5, which is the first element of both lists, if the sum is smaller than 10, then we simply output the sum to the corresponding element in the output list. Let’s say 2+5->7.
If sum is >=10, we leave the mode to the corresponding position in the output list and add 1 to the follow position as we know the sum <=19 (9+9+?1), here ?1 depends on whether there is a 1 from the previous position.
Solution
|
|
Runtime: 68ms
Memory: 14.4MB