码迷,mamicode.com
首页 > 其他好文 > 详细

Add Two Numbers

时间:2015-05-24 06:39:48      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:c++

/**
 * 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 *p=l1,*q=l2;
        int value1,t=0;
        value1=p->val+q->val;
        t=value1/10;
        p->val=value1%10;
        while(p->next!=NULL && q->next!=NULL)
        {
            p=p->next;
            q=q->next;
            value1=p->val+q->val+t;
            t=value1/10;
            p->val=value1%10;
        }
        if(t==1)
        {
            if(p->next!=NULL && q->next==NULL)
            {
                while(p->next!=NULL && t==1)
               {
                   p=p->next;
                   value1=p->val+t;
                   t=value1/10;
                   p->val=value1%10;
                }
                if(t==1)
                {
                    ListNode *L=new ListNode(t);
                    p->next=L;
                }
            }
            else if(p->next==NULL && q->next!=NULL)
            {
                p->next=q->next;
                while(p->next!=NULL && t==1)
               {
                   p=p->next;
                   value1=p->val+t;
                   t=value1/10;
                   p->val=value1%10;
                }
                if(t==1)
                {
                    ListNode *L=new ListNode(t);
                    p->next=L;
                }
            }
            else if(p->next==NULL && q->next==NULL)
            {
                ListNode *L=new ListNode(t);
                p->next=L;
            }
        }
        else if(t==0)
        {
            if(p->next==NULL && q->next!=NULL)
            {
                p->next=q->next;
            }
        }
        
        return l1;
    }
};

//以下是错误的代码

/**
 * 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 *p=l1,*q=l2;
        int value1,t=0;
        value1=p->val+q->val;
        t=value1/10;
        p->val=value1%10;
        while(p->next!=NULL && q->next!=NULL)
        {
            p=p->next;
            q=q->next;
            value1=p->val+q->val+t;
            t=value1/10;
            p->val=value1%10;
        }
        if(t==1)
        {
            if(p->next!=NULL && q->next==NULL)
            {
                while(p->next!=NULL && t==1)
               {
                   p=p->next;
                   value1=p->val+t;
                   t=value1/10;
                   p->val=value1%10;
                }
                if(t==1)
                {
                    ListNode L(t);
                    p->next=&L;
                }
            }
            else if(p->next==NULL && q->next!=NULL)
            {
                p->next=q->next;
                while(p->next!=NULL && t==1)
               {
                   p=p->next;
                   value1=p->val+t;
                   t=value1/10;
                   p->val=value1%10;
                }
                if(t==1)
                {
                    ListNode L(t);
                    p->next=&L;
                }
            }
            else if(p->next==NULL && q->next==NULL)
            {
                ListNode L(t);
                p->next=&L;
            }
        }
        else if(t==0)
        {
            if(p->next==NULL && q->next!=NULL)
            {
                p->next=q->next;
            }
        }
        
        return l1;
    }
};

错误原因:

技术分享

看出来差别了么?就是一个用了结构的指针,一个是结构变量。最后导致了莫名其妙的错误,花费了我上十个小时,这是为什么呢?难道是因为一个位于堆、一个位于栈里?

Add Two Numbers

标签:c++

原文地址:http://yuzwei.blog.51cto.com/10126623/1654527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!