[LeetCode]-002-Add Two Numbers, leetcode-002-add
Web: https://leetcode.com/problems/add-two-numbers/
Question:
Can be used for addition of large numbers,
The values start from the low position and end to end. If the value is greater than or equal to 10, the values are carried.
Tip:
(1) l1 is null or l2 is null
(2) l1 is longer than l2 or l2 is longer than l1
(3) The highest bit in l1 and l2 is still insufficient to represent the sum of the two, that is, you need to create a node.
Solution 1:
Change the l1 value,
If l1 is shorter than l2, l2
In this case, a maximum of one node can be created. The space complexity is O (1), but the original data is changed.
Code:
Https://github.com/LiLane/leetcode/blob/master/java/002-AddTwoNumbers-201504162157.java
Https://github.com/LiLane/leetcode/blob/master/c%2B%2B/002-AddTwoNumbers-201504162202.cpp
Solution 2:
The original data is not changed, and the storage space and value are allocated. The space complexity is O (max (m, n ))
In this way, the code will be more elegant
Code:
Https://github.com/LiLane/leetcode/blob/master/c%2B%2B/002-AddTwoNumbers-201504272115.cpp
Https://github.com/LiLane/leetcode/blob/master/java/002-AddTwoNumbers-201504272122.java