[Leetcode]-002-add Numbers

Source: Internet
Author: User
Tags add numbers

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

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.