Add Two Numbers leecode 刷题之旅

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.

这道题是将两个链表上表示的整型数字相加放到新的一个链表当中。两种思路.

第一种思路就是同时进行两个链表节点的相加,这样避免了将链表节点表示的数分别相加造成的数据溢出。需要注意的是

第一设置进位,表示两节点相加后超过10若超过时,则进1.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//弄清楚节点的表示方法
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    
    struct ListNode *head,*p,*q,*pre,*h;   //定义节点
    int jinwei=0; //进位
    if(l1==NULL) return l2;
    if(l2==NULL) return l1;  //当空链表使返回另一个链表
    
    head =l1;  //设立头节点
    p =head;
    
    while(l1&&l2)   //第一部分,当l1和l2都不是空节点 时
    {
        pre =p;//指向p指针的前向指针
        p->val =l1->val+l2->val+jinwei;     
        jinwei=0;
        if(p->val>9)
        {
            p->val-=10;
            jinwei=1;
        }
        l1 = l1->next;
        l2 = l2->next;
        p=p->next;
    }
    if(!l1&&!l2) //如果l1和l2长度一致,刚好都结束时
    {
        if(jinwei)
        {
            q =(struct ListNode *)malloc(sizeof(struct ListNode));
            q->val = jinwei;
            q->next =NULL;
            pre->next =q;
        }
    }
    else if(l2)
    {
        pre->next =l2;
        h = pre->next;    
         while(h)
           {
                h->val +=jinwei;
                if(h->val>9)
                {
                    h->val -=10;
                    jinwei =1;
                }
                else
                {
                    jinwei =0;
                }
                pre = h;
                h =h->next;
         }
      
     if(jinwei)
     {
            q =(struct ListNode *)malloc(sizeof(struct ListNode));
            q->val =jinwei;
            q->next =NULL;
            pre->next =q;
        }
    }
    else
    {
          pre->next =l1;
          h = pre->next; 
            while(h)
            {
                h->val +=jinwei;
                if(h->val>9)
                {
                    h->val -=10;
                    jinwei=1;
                }
                else
                {
                    jinwei =0;
                }
                pre = h;
                h =h->next;
        }
        if(jinwei)
        {
            q =(struct ListNode *)malloc(sizeof(struct ListNode));
            q->val =jinwei;
            q->next =NULL;
            pre->next =q;
        }
    }
    return head;
   
    
}

第二种思路就是分别两个链表表示的整数相加,得到新的整数,然后新建一个链表,将其得到。不过经过测试,只能在长整型下有效,改进的方法可以将数保存为字符串形式,这样也能解决问题。附上长整型下有效的代码。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//弄清楚节点的表示方法
void addNode(struct ListNode* phead,int value)
{
    struct ListNode *pnew=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode *  p;
    pnew->next=NULL;
    pnew->val=value;
  if(phead->next==NULL)
    phead->next=pnew;
  else
  {
     p=phead;
    while(p->next!=NULL)
     {
       p=p->next;
     }
     p->next=pnew;
  }
}


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) 
{
    long int resL1,resL2;
    long int  k;
    struct ListNode *head,*p; 
    int jinwei=0;
    if(l1==NULL) return l2;
    if(l2==NULL) return l1;
    
    head =l1;
    p =head;
    resL1=0;
    resL2=0;
    k=1;
   while(l1)
   {
       resL1 =resL1+l1->val*k;
       l1 = l1->next;
       k=k*10;
   }
    k=1;
    while(l2)
    {
        resL2 = resL2+l2->val*k;
        l2 = l2->next;
        k=k*10;
    }
    resL1 = resL1+resL2;   //得到两个相加后的值
    while(resL1)
    {
        p->val = resL1%10;
        if(p->next==NULL&&resL1>=10)
        {
           addNode(p,resL1);
        }
        p = p->next;
        resL1 = resL1/10;
    }
    return head;
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值