LeetCode笔记-A2-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.

My Solution:

/**
 * 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=(ListNode*)malloc(sizeof(ListNode));
        int carry=0;
        ListNode* head=(ListNode*)malloc(sizeof(ListNode));
        head=result;
        while (l1!= NULL&&l2!= NULL){
            ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
            temp->val=l1->val+l2->val+carry;
            if (temp->val>=10) {
                carry=1;
                temp->val=temp->val%10;
            } else carry=0;

            result->next=temp;
            result = result->next;

            l1=l1->next;
            l2=l2->next;
        }
        while (l1!= nullptr) {
            if (carry>0){
                ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
                temp->val=l1->val+carry;
                carry=0;
                if (temp->val >= 10 ){
                    carry=1;
                    temp->val%=10;
                }
                result->next=temp;

            }
            else result->next=l1;
            result = result->next;
            l1=l1->next;
        }
        while (l2!= nullptr) {
            if (carry>0){
                ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
                temp->val=l2->val+carry;
                carry=0;
                if (temp->val >= 10 ){
                    carry=1;
                    temp->val%=10;
                }
                result->next=temp;

            }
            else result->next=l2;
            result = result->next;
            l2=l2->next;
        }
        //最高位进位处理
        if (carry>0) {
            ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
            temp->val=1;
            result->next=temp;
            result = result->next;
        }
        result->next= nullptr;
        return head->next;
    }
};

在LeetCode的评论区网友的指导下,简化了一下代码:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* result=(ListNode*)malloc(sizeof(ListNode));
        int carry=0;
        ListNode* head=(ListNode*)malloc(sizeof(ListNode));
        head=result;
        while (l1!= NULL||l2!= NULL){
            ListNode* temp=(ListNode*)malloc(sizeof(ListNode));

            if (l1==NULL)
            {
                temp->val=l2->val+carry;
            }
            else if (l2==NULL)
            {
                temp->val=l1->val+carry;
            }else temp->val=l1->val+l2->val+carry;
            if (temp->val>=10) {
                carry=1;
                temp->val=temp->val%10;
            } else carry=0;

            result->next=temp;
            result = result->next;

            if (l1!=NULL) l1=l1->next;
            if (l2!=NULL) l2=l2->next;
        }

        if (carry>0) {
            ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
            temp->val=1;
            result->next=temp;
            result = result->next;
        }
        result->next= nullptr;
        return head->next;
    }
};

总结:非常简单的链表操作;注意处理一下最高位进位的情况就好(i.e. 1+99=100)
在LeetCode的评论区看到一种比较优雅的写法:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode * l0 = nullptr, * l3 = nullptr;
        int a = 0;

        while( l1 or l2 or a ) {
            if(l1){
                a+=l1->val;
                l1 = l1->next;
            }
            if(l2){
                a+=l2->val;
                l2 = l2->next;
            }

            if(l3) {
                l3->next = new ListNode(a%10);
                l3 = l3->next;
            }
            else {
                l3 = l0 = new ListNode(a%10);
            }
            a /= 10;
        }

        return l0;
    }
};

这个变量a相当巧妙,先保存相加的和,再用%去余数,然后保存进位值,自愧弗如。

猜你喜欢

转载自blog.csdn.net/ken_for_learning/article/details/81734658
今日推荐