• Add Two Numbers


    You are given two linked lists representing two non-negative numbers. 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.

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

    大数相加,只保留各位,进位加到下一节点。

    需注意,判断l1,l2是否为空。若两链表长度不同,需将剩余节点同样作进位处理,若最后剩有进位,需增加新节点。

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    12         if(l1==NULL) return l2;
    13         if(l2==NULL) return l1;
    14         ListNode* res=NULL,*pNode=NULL,*pnext=NULL;
    15         ListNode* p=l1,*q=l2;
    16         int up=0;
    17         while(p!=NULL&&q!=NULL)
    18         {
    19             pnext=new ListNode(p->val+q->val+up);
    20             up=pnext->val/10;
    21             pnext->val=pnext->val%10;
    22             if(res==NULL)
    23               res=pNode=pnext;
    24             else
    25               {
    26                   pNode->next=pnext;
    27                   pNode=pnext;
    28               }
    29             p=p->next;
    30             q=q->next;
    31         }
    32         while(p!=NULL)
    33         {
    34             pnext=new ListNode(p->val+up);
    35             up=pnext->val/10;
    36             pnext->val=pnext->val%10;
    37             pNode->next=pnext;
    38             pNode=pnext;
    39             p=p->next;
    40         }
    41         while(q!=NULL)
    42         {
    43             pnext=new ListNode(q->val+up);
    44             up=pnext->val/10;
    45             pnext->val=pnext->val%10;
    46             pNode->next=pnext;
    47             pNode=pnext;
    48             q=q->next;
    49         }
    50         if(up!=0)
    51         {
    52             pnext=new ListNode(up);
    53              pNode->next=pnext;
    54             pNode=pnext;
    55         }
    56         return res;
    57     }
    58 };

     另一种写法

    /**
     * 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) {
            int add = 0;
            ListNode *res = NULL;
            if(l1==NULL&&l2==NULL)
                return res;
            ListNode *p = new ListNode(0);
            res = p;
            while(l1!=NULL&&l2!=NULL)
            {
                int val = l1->val+l2->val+add;
                add = 0;
                p->next=new ListNode(val%10);
                add=val/10;
                p=p->next;
                l1=l1->next;
                l2=l2->next;
            }
            while(l1!=NULL)
            {
                int val = l1->val+add;
                add=0;
                p->next=new ListNode(val%10);
                add=val/10;
                p=p->next;
                l1=l1->next;
            }
            while(l2!=NULL)
            {
                int val = l2->val+add;
                add=0;
                p->next=new ListNode(val%10);
                add=val/10;
                p=p->next;
                l2=l2->next;
            }
            if(add!=0)
            {
                p->next=new ListNode(add);
            }
            return res->next;
        }
    };
  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/zl1991/p/4686352.html
Copyright © 2020-2023  润新知