leetcode 2

2017-08-27  本文已影响0人  cde99bf0b5b1
/**
 * 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,*p2,*p3,*res;
        p1 = l1;
        p2 = l2;
        res = new ListNode(0);
        p3 = res;
        for( ; (p1!=NULL)&&(p2!=NULL); p1 = p1->next, p2 = p2->next){
            int k = p2->val + p1->val + p3->val;
            if(k >= 10){
                p3->val = k - 10;
                p3->next = new ListNode(1);
                p3 = p3->next;
            }
            else{
                p3->val = k;
                if((p2->next != NULL) && (p1->next != NULL)){
                    p3->next = new ListNode(0);
                    p3 = p3->next;
                }
                else if((p2->next == NULL)&&(p1->next == NULL)){
                    p3->next = NULL;
                }
                else{
                    p3->next = new ListNode(0);
                    p3 = p3->next;
                }
            }
        }
        int temp;
        if((p1==NULL)&&(p2==NULL)){
            return res;
        }
        else if(p1 != NULL){
            for(; p1 != NULL; p1 = p1->next){
                temp = p1->val + p3->val;
                if(temp >= 10){
                    p3->val = temp - 10;
                    p3->next = new ListNode(1);
                    p3 = p3->next;
                }
                else{
                    if(p1->next == NULL){
                        p3->val = temp;
                        p3->next = NULL;
                        break;
                    }
                    else{
                        p3->val = temp;
                        p3->next = new ListNode(0);
                        p3 = p3->next;
                    }
                }
            }
        }
        else if(p2 != NULL){
            for(; p2 != NULL; p2 = p2->next){
                temp = p2->val + p3->val;
                if(temp >= 10){
                    p3->val = temp - 10;
                    p3->next = new ListNode(1);
                    p3 = p3->next;
                }
                else{
                    if(p2->next == NULL){
                        p3->val = temp;
                        p3->next = NULL;
                        break;
                    }
                    else{
                        p3->val = temp;
                        p3->next = new ListNode(0);
                        p3 = p3->next;
                    }
                }
            }
        }
        return res;
    }
};

高阶版本

/**
 * 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) {
        static const int R = 10;
        ListNode *p1, *p2, *p3;
        ListNode *h3, *t3;
        p1 = l1;
        p2 = l2;
        h3 = t3 = NULL;
        while (p1 != NULL && p2 != NULL) {
            p3 = new ListNode(p1->val + p2->val);
            if (t3 == NULL) {
                h3 = t3 = p3;
            } else {
                t3->next = p3;
                t3 = p3;
            }
            p1 = p1->next;
            p2 = p2->next;
        }
        while (p1 != NULL) {
            t3->next = new ListNode(p1->val);
            t3 = t3->next;
            p1 = p1->next;
        }
        while (p2 != NULL) {
            t3->next = new ListNode(p2->val);
            t3 = t3->next;
            p2 = p2->next;
        }
        
        p3 = h3;
        while (p3->next != NULL) {
            p3->next->val += p3->val / R;
            p3->val %= R;
            p3 = p3->next;
        }
        while (p3->next == NULL && p3->val >= R) {
            p3->next = new ListNode(p3->val / R);
            p3->val %= R;
            p3 = p3->next;
        }
        
        return h3;
    }
};
上一篇下一篇

猜你喜欢

热点阅读