Accepted 32ms c++ solution, easy understand.


  • 0
    Z
    /**
     * 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* result = new ListNode(0);
            ListNode* r = result;
            ListNode* p = l1;
            ListNode* q = l2;
            int remain = 0;
            while(p&&q)
            {
                ListNode* k = NULL;
                int tmp = p->val+q->val+remain;
                if(tmp>9)
                {
                    k = new ListNode(tmp-10);
                    remain = 1;
                }
                else
                {
                    k = new ListNode(tmp);
                    remain = 0;
                }
                r->next = k;
                r = r->next;
                p = p->next;
                q = q->next;
            }
            while(p)
            {
                ListNode* k = NULL;
                int tmp = p->val+remain;
                if(tmp>9)
                {
                    k = new ListNode(tmp-10);
                    remain = 1;
                }
                else
                {
                    k = new ListNode(tmp);
                    remain = 0;
                }
                r->next = k;
                r = r->next;
                p = p->next;
            }
            while(q)
            {
                ListNode* k = NULL;
                int tmp = q->val+remain;
                if(tmp>9)
                {
                    k = new ListNode(tmp-10);
                    remain = 1;
                }
                else
                {
                    k = new ListNode(tmp);
                    remain = 0;
                }
                r->next = k;
                r = r->next;
                q = q->next;
            }
            if(remain==1)
            {
                ListNode* k = new ListNode(1);
                r->next = k;
            }
            return result->next;
        }
    };

  • 0

    Same idea but less external variables, also 32ms.

    class Solution {
    public:
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
            if (l1 == NULL)
                return l2;
            else if (l2 == NULL)
                return l1;
            ListNode *res = l1;
            int tmp = 0;
            while (true) {
                if ((l1->val += l2->val + tmp) > 9) {
                    tmp = 1;
                    l1->val -= 10;
                }
                else
                    tmp = 0;
                if (l1->next == NULL) {
                    l1->next = l2->next;
                    break;
                }
                else if (l2->next == NULL)
                    break;
                l1 = l1->next;
                l2 = l2->next;
            }
            while (l1->next != NULL && tmp) {
                l1 = l1->next;
                if ((l1->val += tmp) > 9) {
                    tmp = 1;
                    l1->val -= 10;
                }
                else
                    return res;
            }
            if (tmp) {
                l2->val = tmp;
                l2->next = NULL;
                l1->next = l2;
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.