Leetcode -- Add Two Numbers (0002 ),

Source: Internet
Author: User

Leetcode -- Add Two Numbers (0002 ),

Reprinted Please note: http://www.cnblogs.com/igoslly/p/8672467.html

 

Let's take a look at the question:

You are given twoNon-emptyLinked lists representing two non-negative integers. The digits are stored inReverse orderAnd each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain in any leading zero, cannot the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8Explanation: 342 + 465 = 807.

The question actually refers to storing two numbers in the reverse order of the linked list.

After summation, the results are also output in reverse order.

Ideas:

1. Because of the two numbers, the idea of the chain table-> int number is basically hopeless and may be out of the range.

2. The linked list has been in reverse order and provides a good operation mode of "adding corresponding bits and carrying forward ".

Note:

1. Length of List1 and List2

2. After a List is complete, you can calculate another List

3. Consider the carry relationship, for example, 9 + 99999

4. In the final carry, an additional val = 1 node is required.

 

Implementation Method 1 (initial ):

Method 1 is the result of drawing the gourd according to the above idea.

But in fact, there are too many duplicates in the code in this question, and we always need to use the pre variable to track the front node, which is redundant.

/*** Definition for singly-linked list. * struct ListNode {* int val; * ListNode * next; * ListNode (int x): val (x), next (NULL ){}*}; */class Solution {public: ListNode * addTwoNumbers (ListNode * l1, ListNode * l2) {ListNode * p1 = l1, * p2 = l2, * pre = p1; int up = 0; // return the result linked list with node l1 in the valid range of l1 and l2, and update up carry while (p1 & p2) {p1-> val + = p2-> val + up; up = p1-> val/10; if (up = 1) {p1-> val-= 10 ;} pre = p1; p1 = p1-> next; P2 = p2-> next;} // when l1 ends, the last pre element is connected to the back of l2 if (p1 = NULL) {pre-> next = p2; while (p2! = NULL) {p2-> val + = up; up = p2-> val/10; if (up = 1) {p2-> val-= 10 ;} pre = p2; p2 = p2-> next ;}// when l2 ends, calculate l1 if (p2 = NULL) {while (p1! = NULL) {p1-> val + = up; up = p1-> val/10; if (up = 1) {p1-> val-= 10 ;} pre = p1; p1 = p1-> next ;}// when the computation is over and up = 1, it indicates one or more digits, add if (up = 1) {pre-> next = new ListNode (1) ;}return l1 ;}} to the ListNode with the newly created val = 1 ;}};

 

 

Implementation Method 2 (optimization of method 1 ):

Relatively concise

/*** Definition for singly-linked list. * struct ListNode {* int val; * ListNode * next; * ListNode (int x): val (x), next (NULL ){}*}; */class Solution {public: ListNode * addTwoNumbers (ListNode * l1, ListNode * l2) {int carry = 0; ListNode * listNode = new ListNode (0 ); listNode * p1 = l1, * p2 = l2, * p3 = listNode; // modify the judgment condition from & to | while (p1! = NULL | p2! = NULL) {// Add the judgment of p1 and p2 in the while LOOP, saving the case of separate List after a List is completed if (p1! = NULL) {carry + = p1-> val; p1 = p1-> next;} if (p2! = NULL) {carry + = p2-> val; p2 = p2-> next;} p3-> next = new ListNode (carry % 10); p3 = p3-> next; carry/= 10;} // because a separate result linked list is created, you do not need to use the pre-node if (carry = 1) p3-> next = new ListNode (1); return listNode-> next ;}};

 

Related Article

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.