LeetCode题解:Add Two Numbers

Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

思路:

中小学竞赛题。

题解:

/**
 * 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* i1 = l1;
        ListNode* i2 = l2;
        
        ListNode* ret = new ListNode(0);
        ListNode* ret_last = ret;
        
        bool carry = false;
        
        while(i1 != nullptr || i2 != nullptr)
        {
            int s = 0;

            if (i1 != nullptr)
                s = i1->val, i1 = i1->next;
            
            if (i2 != nullptr)
                s += i2->val, i2 = i2->next;
            
            s += carry;

            ret_last->next = new ListNode(s % 10);
            ret_last = ret_last->next;

            carry = (s >= 10);
        }
        
        if (carry)
            ret_last->next = new ListNode(1);
        
        ret_last = ret->next;
        delete ret;
        
        return ret_last;
    }
};


你可能感兴趣的:(数据结构和算法)