2. Add Two Numbers

不是很难的一道题,就是list操作,但是细节总是写不对。

1 循环时是先while(l1 != NULL && l2 != NULL)然后在加剩下的list,但是发现这个时候也要考虑进位,所以把长list的后半段留下来并不能直接加在返回list的后面,所以还是用while(l1 != NULL || l2 != NULL)把两个list都遍历完,再处理剩下的情况,

2 但是使用sum来记录总和,而不是单记录carry再多存一个res,这样会更简洁也节省空间。

3 我最开始的时候在开头判断了一个 if (l1->val == 0) return l2; if (l2->val == 0) return l1; 因为想判断一个为0的情况,这样可以直接返回另一个list,但是忘记了list是逆序,第一个数为0,并不代表这个list表示的是0.

4 不要用ListNode* head = NULL, end = NULL; 这样会把end变成ListNode类型而不是ListNode*

最开始的版本:

/**
 * 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* head = NULL; 
        ListNode* end = NULL;// don't use ListNode* head = NULL, end = NULL; will define end as ListNode instead of ListNode*
        int carry = 0;
        while (l1 != NULL && l2 != NULL) {
            int res = l1->val + l2->val + carry;
            ListNode* node;
            if (res > 9) {
                node = new ListNode(res % 10);
                cout << node->val << endl;
            }
            else {
                node= new ListNode(res);
                cout << node->val <<endl;
            }
            if (head == NULL) {
                head = node;
                end = node;
            }
            else {
                end->next = node;
                end = end->next;
            }
            carry = res/10;
            l1 = l1->next;
            l2 = l2->next;
        }
        while (l1 != NULL) {
            int res = l1->val + carry;
            ListNode* node;
            if (res > 9) {
                node = new ListNode(res % 10);
            }
            else {
                node = new ListNode(res);
            }
            if (head == NULL) {
                head = node;
                end = node;
            }
            else {
                end->next = node;
                end = end->next;
            }
            carry = res/10;
            l1 = l1->next;
        }
        while (l2 != NULL) {
            int res = l2->val + carry;
            ListNode* node;
            if (res > 9) {
                node = new ListNode(res % 10);
            }
            else {
                node = new ListNode(res);
            }
            if (head == NULL) {
                head = node;
                end = node;
            }
            else {
                end->next = node;
                end = end->next;
            }
            carry = res/10;
            l2 = l2->next;
        }
        if (carry) {//don't forget to add this node when carry is not 0 
           ListNode* node = new ListNode(carry);
           end->next = node;
            end = end->next;
        }
        return head;
    }
};


修改后更简洁的版本:

/**
 * 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* head = NULL; 
        ListNode* end = NULL;
        int sum = 0;
        while (l1 != NULL || l2 != NULL) {
            if (l1 != NULL) {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL) {
                sum += l2->val;
                l2 = l2->next;
            } 
            ListNode* node = new ListNode(sum%10);
            sum = sum / 10;
            if (head == NULL) {
                head = node;
                end = node;
            }
            else {
                end->next = node;
                end = end->next;
            }
        }
        while (sum) {
            ListNode* node = new ListNode(sum%10);
            end->next = node;
            end = end->next;
            sum = sum /10;
        }
        return head;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值