leetCode # 2 simple questions

Title: given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit. If we add up these two numbers, it will return a new list and to represent them. You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

Solution: it is a pointer to the head of the linked list, traversing two back, and the remainder count, record it into place. If L1 first traversed to the end, then put the last pointer points to L1 L2 current next, i.e. L2 is connected to the remaining part of the end of L1. This saves a lot of space to open up new Node, and then continue to use the current value of L1 and carry summing Node recorded a carry. If at this time there is a carry to the end of L1, to new Node add a new end on the line. In order to finish quickly, the code a bit ugly, will see, later if not lazy, then re-optimize it.

/**
 * 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 toNextNum = 0;
        ListNode* beginNode = l1;
        ListNode* lastNode = l1;
        while(l1 && l2){
            if (l1->val + l2->val + toNextNum >= 10){
                l1->val = l2->val = (l1->val + l2->val + toNextNum) % 10;
                toNextNum = 1;
            }else{
                l1->val = l2->val = (l1->val + l2->val + toNextNum);
                toNextNum = 0;
            }
            lastNode = l1;
            l1 = l1->next;
            l2 = l2->next;
        }
        if (!l1){
            if (!l2){
                if (toNextNum > 0)
                    lastNode->next = new ListNode(1);
            }else{
                lastNode->next = l2;
                l1 = lastNode = lastNode->next;
                while(toNextNum > 0){
                    if (l1){
                        if (l1->val + toNextNum >= 10){
                            l1->val = (l1->val + toNextNum) % 10;
                            toNextNum = 1;
                            lastNode = l1;
                            l1 = l1->next;
                        }else{
                            l1->val = (l1->val + toNextNum);
                            toNextNum = 0;
                        }
                    }else{
                        lastNode->next = new ListNode(1);
                        toNextNum = 0;
                    }
                }
            }
        }else{
            lastNode = l1;
            while(toNextNum > 0){
                if (l1){
                    if (l1->val + toNextNum >= 10){
                        l1->val = (l1->val + toNextNum) % 10;
                        toNextNum = 1;
                        lastNode = l1;
                        l1 = l1->next;
                    }else{
                        l1->val = (l1->val + toNextNum);
                        toNextNum = 0;
                    }
                }else{
                    lastNode->next = new ListNode(1);
                    toNextNum = 0;
                }
            }
        }
        return beginNode;
    }
};

 

Guess you like

Origin www.cnblogs.com/error408/p/11546382.html