https://leetcode.com/problems/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* res = new ListNode(0);
int tmp = 0,cnt = 0;
ListNode * result = res;
while(l1||l2||cnt){
tmp = cnt;
tmp += (l1?l1->val:0) + (l2?l2->val:0);
cnt = tmp / 10;
tmp %= 10;
if(l1)l1=l1->next;
if(l2)l2=l2->next;
res->val = tmp;
if(l1||l2||cnt){
res->next = new ListNode(0);
res = res->next;
}
}
return result;
}
};

  

LeetCode 2 Add Two Numbers 模拟,读题 难度:0的更多相关文章

  1. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  2. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  5. LeetCode 面试:Add Two Numbers

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

  6. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  7. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  8. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

随机推荐

  1. shell 从1加到100

  2. ubuntu 14.04 vsftpd安装问题

    sudo apt-get install vsftpd; 打开允许访问用户名 local_enable=YES write_enable=YES chroot_list_file=/etc/vsftp ...

  3. web前端之HTML的前世今生

    一个尖括号   < 一个尖括号能干什么    < ? 你可以编出一顶帽子  <(:-p 或一张笑脸    :-> 或诉说一份爱   <3 或者更直接一些 <!DOC ...

  4. [原创]DC-DC输出端加电压会烧毁

      在调试智能钥匙连续开锁出现故障的问题排查过程中,为了对比模拟开关TS5A3166对于开锁数据通信的影响,尝试短接模拟开关的输入输出脚,未曾想乌龙了一把,错把DC-DC芯片输入输出短接了(两者都是S ...

  5. Cocos2d-x lua 游戏中的菜单(Menu)

    菜单相关类包含 菜单(Menu)类 和 菜单项( MenuItem )类 Menu类图(派生于Layer) ref <-- Node <-Layer <-- Menu MenuIte ...

  6. PCA9554

    参考资料: 1. Texas Instruments PCA9554简介 2. PCA9554文档下载 3. PCA9554 Data Sheet 知识点: ● PCA9554是具有中断输出和配置寄存 ...

  7. [git] git代理及常用命令,远程桌面代理

     1.代理 公司只能内网,上外网只能用代理,坑货! 2. 更新代码命令    1)下载代码:git clone ------------    2) 指定目录: cd 文件名    3)git add ...

  8. 物理Data Guard的日常维护

    关闭顺序 1. 首先关闭primary数据库 [oracle@Master admin]$ sqlplus / as sysdba SQL> shutdown immediate 2. 关闭st ...

  9. Spring基于注解ehCache缓存整合

    注解的使用参照:http://blog.csdn.net/wjacketcn/article/details/50945887 (侵删) ehCache是纯java实现的缓存组件,spring从3.1 ...

  10. iOS9 3DTouch开发

    在iOS 9中,新iPhone将第三维度添加到了用户界面. 用户现在可以用力摁下主屏按钮来快速调出应用提供的功能菜单. 在应用中,用户现在可以用力摁下视图以查看更多内容的预览并且快速访问一些功能. 想 ...