2. Add Two Numbers

2. Add Two Numbers

题目

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and 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 any leading zero, except the number 0 itself.

Example

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

解析

  • ac的思路三个while()循环感觉很麻烦
  • 可以将链表的长度求出来,以长的为标准,另一个以0补全
/**
 * 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) {
        if (!l1)
        {
            return l2;
        }
        if (!l2)
        {
            return l1;
        }

        ListNode* head = new ListNode(0),*pre=head;
        ListNode* cur = NULL;
        bool flag = false; //进位标志

        int sum = 0;
        while (l1!=NULL&&l2!=NULL)
        {
            sum = l1->val + l2->val;
            if (flag)
            {
                sum = sum + 1;
                flag = false;
            }       
            if (sum>=10)
            {
                sum = sum % 10;
                flag = true;
            }
            cur = new ListNode(sum);
            pre->next=cur;
            pre = pre->next;
            l1 = l1->next;
            l2 = l2->next;
        }


        while (l2 != NULL)
        {
            sum = l2->val;
            if (flag)
            {
                sum = sum + 1;
                flag = false;
            }
            if (sum >= 10)
            {
                sum = sum % 10;
                flag = true;
            }
            cur = new ListNode(sum);
            pre->next = cur;
            pre = pre->next;
            l2 = l2->next;
        }


        while (l1 != NULL)
        {
            sum = l1->val;
            if (flag)
            {
                sum = sum + 1;
                flag = false;
            }
            if (sum >= 10)
            {
                sum = sum % 10;
                flag = true;
            }
            cur = new ListNode(sum);
            pre->next = cur;
            pre = pre->next;
            l1 = l1->next;
        }
        if (flag)
        {
            cur = new ListNode(1);
            pre->next = cur;
        }
        return head->next;
    }
};

 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        
        ListNode* result = NULL;
        ListNode* result_tail = NULL;
        
        int carry = 0;
        while(l1 || l2){

            // default value of each list's current node to 0
            //
            int value1 = 0;
            int value2 = 0;

            // overwite default value of 0 if the list is NOT exhausted
            // and move each list forward in preparation for next iteration
            //
            if (l1){
                value1 = l1->val;
                l1 = l1->next;
            }
            if (l2){
                value2 = l2->val;
                l2 = l2->next;
            }

            // add the values at this digit position ( +1 for carry if applicable )
            // also keep track of the updated carry in preparation for the next iteration
            //
            int sum = value1 + value2 + carry;
            
            if (sum >= 10){
                carry = 1;
                sum %= 10;
            } else {
                carry = 0;
            }

            // include sum of current digit onto the result
            //
            if (!result){
                result = new ListNode(sum);
                result_tail = result;
            } else {
                result_tail->next = new ListNode(sum);
                result_tail = result_tail->next;
            }
        }

        // the very last sum might have a carry, add a new node if needed
        //
        if (carry){
            result_tail->next = new ListNode(carry);
        }
        
        return result;
    }

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8278965.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞