【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

代码

/**
 * 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 dummy(-1);
        ListNode *p = &dummy,*p1 = l1,*p2 = l2;
        int carry = 0;
        while(p1!=NULL || p2!=NULL){
            const int v1 = p1==NULL?0:p1->val, v2 = p2==NULL?0:p2->val, v = (v1+v2+carry)%10;
            carry = (v1+v2+carry)/10;
            p->next = new ListNode(v);
            p = p->next;
            p1 = p1==NULL ? NULL : p1->next;
            p2 = p2==NULL ? NULL : p2->next;
        }
        if ( carry > 0 ) p->next = new ListNode(carry);
        return dummy.next;
    }
};

Tips

核心在于判断while停止条件:直到l1和l2都走完了才退出;如果l1或者l2先走完了,就当该位是0。

上面这种思路的好处是可以简化代码。

时间: 2024-10-10 14:20:11

【Add Two Numbers】的相关文章

leetcode 【 Add Two Numbers 】 python 实现

题目: 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 -&

CF401D 【Roman and Numbers】

题意将n(n<=10^18)的各位数字重新排列(不允许有前导零)  求  可以构造几个mod m等于0的数字解法状压f[S][k] 表示选用的位数集合为S,mod m 为k的方案数注意不能有前导0但是这样做是有缺陷的状压本质上是将每个数按下标强行看作不同的数因此有重复统计的情况比如n=11,方案只有1种,状压会有2种根据多重集合的排列,如果一个数字出现了cnt次,那么答案会被重复计算cnt!次,答案需要除以cnt!上代码 #include<iostream> #include<cs

题解 CF817C 【Really Big Numbers】

题目链接:CF817C 前置算法 : 二分 我们先考虑如何得到答案,若最小满足\(x\)减去其各数位之和后大于\(s\) \(ans = n - x + 1\) 我们只要打个表就可以发现\(:\) 若\(x < y\)则\(|x| \leq |y|\) \((\)设\(|x|\)表示\(x\)减去其各数位之和\()\) 证明就不写了 说明答案是递增的, 那就用二分 我们二分出最小满足\(|x|\)大于\(s\) \(check\)函数就根据题意所写 如果找不到\(|x| \leq s\)就输出\

【LeetCode】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 ->

【leetcode刷题笔记】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 ->

【leetcode】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 -&

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

poj 3252 Round Numbers 【推导&#183;排列组合】

以sample为例子 [2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1] 即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1] 所以关键是给定一个X,求出Rn[0,X] 现在假设X=10100100  这个X的二进制总共是8位,任何一个小于8位的二进制都小于X 第一部分,求出长度为[0,7]区间内的二进制是RoundNumber的个数  对于一个长度为Len的二进制(最高位为1),如何求出他的Rou