题目链接

  题目要求:

  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) {
if(!l1)
return l2;
else if(!l2)
return l1; ListNode *dummy = new ListNode();
ListNode *head = nullptr, *start = dummy;
int carry = ;
while(l1 || l2)
{
if(l1 && l2)
{
int sum = l1->val + l2->val + carry;
l1->val = sum % ;
carry = sum / ;
start->next = l1;
l1 = l1->next;
l2 = l2->next;
}
else if(l1)
{
int sum = l1->val + carry;
l1->val = sum % ;
carry = sum / ;
start->next = l1;
l1 = l1->next;
}
else
{
int sum = l2->val + carry;
l2->val = sum % ;
carry = sum / ;
start->next = l2;
l2 = l2->next;
} start = start->next;
} if(carry)
{
ListNode *node = new ListNode();
start->next = node;
} head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

LeetCode之“链表”:Add Two Numbers的更多相关文章

  1. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  2. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  3. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  4. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  6. LeetCode OJ:Add Two Numbers (相加链表之数)

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

  7. 【LeetCode】2.Add Two Numbers 链表数相加

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

  8. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

  9. leetcode题解2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  10. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

随机推荐

  1. How to work with my desktop and laptop

    introduction Two years ago, I got a laptop Lenovo Y500, I am still using it now. Recently, I bought ...

  2. 剑指Offer——关于劳动合同,这6件事毕业生必须知道!

    剑指Offer--关于劳动合同,这6件事毕业生必须知道!   求职找工作,不少人拿到劳动合同的那刻,可能连合同内容都没看清,就挥着笔杆子"签签签".别急!劳动合同包含哪些条款你清楚 ...

  3. 初识gd库

    必备基础 开启GD拓展 列表使用 获取图片信息代码 图片详细信息 特效函数 示例 运行结果 分析 获取图片基本信息 获取图片宽度 获取图片高度 获取图片后缀名 获取图片mime类型 操作图片 添加文字 ...

  4. UIKit视图动画的微扩展

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 好久没写blog了,还不快快写来- ;] 我们知道在UIKit ...

  5. Mysql创建、删除用户、用户管理等相关:转载http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html

    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 ...

  6. Java并发框架——AQS之原子性如何保证?

    在研究AQS框架时,会发现这个类很多地方都使用了CAS操作,在并发实现中CAS操作必须具备原子性,而且是硬件级别的原子性,java被隔离在硬件之上,明显力不从心,这时为了能直接操作操作系统层面,肯定要 ...

  7. 多线程并发之java内存模型JMM

    多线程概念的引入是人类又一次有效压寨计算机的体现,而且这也是非常有必要的,因为一般运算过程中涉及到数据的读取,例如从磁盘.其他系统.数据库等,CPU的运算速度与数据读取速度有一个严重的不平衡,期间如果 ...

  8. 有奖试读—Windows PowerShell实战指南(第2版)

    为什么要学PowerShell? Windows用户都已习惯于使用图形化界面去完成工作,因为GUI总能轻易地实现很多功能,并且不需要记住很多命令.使得短时间学会一种工具成为可能. 但是不幸的是,GUI ...

  9. 22 Notification样式设置内部按钮点击事件

    package com.exam1ple.demo1; import android.app.Activity; import android.app.NotificationManager; imp ...

  10. 集成JPA+springmvc+spring+EJB中的Java EE应用

    EJB是sun的JavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序.凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台.EJB (Enterprise Jav ...