[LeetCode]-002-Add Two Numbers, leetcode-002-add

Source: Internet
Author: User

[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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.