Add Two Number - Leetcode

简要描述:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

  1. 当链表l1和l2有一个为空的时候,则将不为空的那个链表的后面节点的val都存入到firstNode链表的结尾处。(注意:如果carry的值为1,应该将其与不为空的链表的第一个循环节点的val相加,然后将carry置为0)
  2. 当链表l1和l2都为空的时候,也需要注意此时的carry是否为1。如果是1的话,则需在创建一个新的节点来存储carry的值(这里相当于加法进一位,这里的数字顺序是倒序)。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
 int min(int a,int b)
 {
     return a<b?a:b;
 }

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int carry=0,x=0;
        ListNode* firstNode1=l1;
        ListNode* firstNode2=l2;
        x=l1->val+l2->val;
        if(x>=10) {x=x-10;carry=1;}
        ListNode* firstNode=new ListNode(x);    //这里是生成新链表的头指针
        ListNode* node=firstNode;
        while(firstNode1->next!=NULL&&firstNode2->next!=NULL)//链表l1和l2的指针都
             //不为空的时候,求和
        {
            firstNode1=firstNode1->next;
            firstNode2=firstNode2->next;
            x=firstNode1->val+firstNode2->val+carry;
            if(x>=10) {x=x-10; carry=1;}
            else carry=0;
            ListNode* newNode=new ListNode(x);
            node->next=newNode;
            node=newNode;
        }
        //当其中一个链表为空时,则将另外一个不为空的链表每个节点的
        //值存入到新链表的,这里需要注意的是carry的值
        if(firstNode1->next==NULL&&firstNode2->next!=NULL) 
        {
            while(firstNode2->next!=NULL){
                firstNode2=firstNode2->next;
                if(carry==1) {
                    x=firstNode2->val+carry;
                    carry=0;
                    if(x>=10)
                        {x=x-10;carry=1;}
                }
                else x=firstNode2->val;
                ListNode* newNode=new ListNode(x);
                node->next=newNode;
                node=newNode;
            }

        }
         else if(firstNode1->next!=NULL&&firstNode2->next==NULL)
        {
            while(firstNode1->next!=NULL){
                firstNode1=firstNode1->next;
                if(carry==1) {
                    x=firstNode1->val+carry;
                    carry=0;
                    if(x>=10)
                        {x=x-10;carry=1;}
                }
                else x=firstNode1->val;
                ListNode* newNode=new ListNode(x);
                node->next=newNode;
                node=newNode;
            }

        }
        //当两个链表都为空的时候,需要注意此时carry的值是否1,
        //如果等于1的话,就需要在新链表中再建一项,将其存入。
        //如果等于0,说明此时的加法操作已经结束,结果已经出来
        if(firstNode1->next==NULL&&firstNode2->next==NULL&&carry==1)
        {
           ListNode* newNode=new ListNode(carry);
           node->next=newNode;
           node=newNode;
        }
        return firstNode;
    }
};

总结:在做这道题的时候,笔者在一开始将函数参数中的 l1, l2当成数组来处理,这是不正确的。这里应该表示的是链表的头指针。链表每个节点之间的联系是靠节点结构中的ListNode *next来寻找,而与数组顺序无关。
第二点疏忽是,在一个链表为空另一个链表不为空的情况下,只注意到了从之前的循环得来的carry值是否等于1,却忽略了在不为空链表后面的节点与carry相加后的值是否需要进位。
第三点疏忽是,在两个链表都为空,即二者的节点均已相加结束后,忽略了是否存在进位(carry=1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值