Website: https://leetcode.com/problems/add-two-numbers/
Test instructions
can be used to enlarge the number of additions,
From the low start 22 add, if greater than or equal to 10, the rounding
Tips:
(1) L1 is null or L2 is null
(2) L1 longer than L2 or L2 than L1
(3) The highest bit in L1 and L2 is still insufficient to indicate the and of the two, that is, a new node is required.
Solution 1:
Change the value of the L1,
If the L1 is shorter than the L2, it is connected L2
This situation creates up to 1 new nodes with an O (1) space complexity, but changes the original data
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:
Do not change the original data and allocate space storage and values. Space complexity is O (max (m,n))
This way of thinking, the code will be 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
[Leetcode]-002-add Numbers