leetoj Add Two Numbers

原创 2015年07月10日 12:34:27

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

这题的做法就是链表的基本操作,陷阱有:

1.进位问题,特别是当最后一个数字为9,然后有进位的时候

2.两条链表的长度不同的时候

/**
 * 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 sum=0;
        int c=0;
        if(l1==NULL)
            return l1;
        if(l2==NULL)
            return l2;
        ListNode* p;
        sum=l1->val+l2->val;
        c=sum/10;
        sum%=10;
        ListNode* root=new ListNode(sum);
        p=root;
        l1=l1->next;
        l2=l2->next;
        while(l1&&l2)
        {
                sum=l1->val+l2->val+c;
                c=sum/10;
                sum%=10;
                ListNode* q=new ListNode(sum);
                l1=l1->next;
                l2=l2->next;
                p->next=q;
                p=q;
        }
        if(l1)
        {
            while(l1)
            {
                sum=l1->val+c;
                c=sum/10;
                sum%=10;
                ListNode* q=new ListNode(sum);
                l1=l1->next;
                p->next=q;
                p=q;    
            }
        }
        else if(l2)
        {
            while(l2)
            {
                sum=l2->val+c;
                c=sum/10;
                sum%=10;
                ListNode* q=new ListNode(sum);
                l2=l2->next;
                p->next=q;
                p=q;    
            }
        }
        if(c!=0)
        {
            ListNode* q=new ListNode(c);
            p->next=q;
            p=q;    
        }
        return root;
    }
};         
版权声明:可以随意转载

相关文章推荐

[LeetCode] Add two numbers

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode...

LeetCode 2 - Add Two Numbers

问题简述 Add Two Numbers You are given two linked lists representing two non-negative numbe...

LeetCode 开刷2 ADD Two Numbers

趁着还有热情,接着刷。 题目: You are given two linked lists representing two non-negative numbers. The digits ...

[LeetCode][2]Add Two Numbers解析 -Java实现

Q: You are given two linked lists representing two non-negative numbers. The digits are stored in re...

【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】

有两个单链表,代表两个非负数,每一个节点代表一个数位,数字是反向存储的,即第一个结点表示最低位,最后一个结点表示最高位。求两个数的相加和,并且以链表形式返回。对两个链表都从第一个开始处理,进行相加,结...

leetcode Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

LeetCode 2. Add Two Numbers (C++/python/js实现)

将链表所代表的两个数相加,注意维护仅为

Add Two Numbers

leetcode 2 Add Two Numbers Add Two Numbers My Submissions QuestionEditorial Solution You ...

2 Add Two Numbers

2 Add Two Numbers语言:java题目:即给定两个非负整数的链表,注意两个链表存放数是倒着存的。求出两数之和,并且以链表的形式输出。考虑点: 1用两个链表存放两个数,每个节点存放数的每一...

LeetCode(2)Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)