题目

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

链接

https://leetcode.com/problems/add-two-numbers/

答案

1、直接按顺序加就行,保存进位carry

2、到最后还需要考虑进位carry是否为0

代码

 /**
* 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 == NULL)
{
return l2;
} if(l2 == NULL)
{
return l1;
} ListNode *ans = NULL;
ListNode *point = NULL;
int carry = ;
int sum = ;
while(l1 != NULL && l2 != NULL)
{
sum = carry + l1->val + l2->val;
carry = sum / ;
sum = sum % ; ListNode *value = new ListNode(sum);
if(ans == NULL)
{
ans = value;
} if(point != NULL)
{
point->next = value;
}
point = value; l1 = l1->next;
l2 = l2->next;
} while(l1 != NULL)
{
sum = carry + l1->val;
carry = sum / ;
sum = sum % ;
ListNode *value = new ListNode(sum);
point->next = value;
point = value; l1 = l1->next;
} while(l2 != NULL)
{
sum = carry + l2->val;
carry = sum / ;
sum = sum % ;
ListNode *value = new ListNode(sum);
point->next = value;
point = value; l2 = l2->next;
} if(carry != )
{
ListNode *value = new ListNode(carry);
point->next = value;
point = value;
} return ans;
}
};

leetcode-【中等题】2. Add Two Numbers的更多相关文章

  1. LeetCode第二题:Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  2. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  3. LeetCode算法题-Sum of Square Numbers(Java实现)

    这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...

  4. leetcode 中等题(1)

    2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  5. LeetCode解题笔记 - 2. Add Two Numbers

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

  6. leetcode中等题

    # Title Solution Acceptance Difficulty Frequency     1 Two Sum       44.5% Easy     2 Add Two Number ...

  7. LeetCode第四题,Add Two Numbers

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

  8. 【LeetCode刷题系列 - 002题】Add Two Numbers

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

  9. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  10. LeetCode(2)Add Two Numbers

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

随机推荐

  1. 2017年1月4日-linux学习

    老男孩Linux运维实战培训 1.EmEditor文本编辑工具,我用Notepad++也很好用 2.win32文本比较工具使用技巧,我用svn也很好用 3.安装vmware软件及新建配置虚拟机视频,已 ...

  2. 学韩顺平老师linux教程--笔记

    第二讲:1.startx  进入图形界面2.shutdown -h now 立刻进行关机3.shutdown -r now 现在重新启动计算机4.reboot          现在重新启动计算机5. ...

  3. sql查询语句

    //查询表的字段名和字段类型select column_name,data_type from information_schema.columns where table_name = '表名' / ...

  4. xmind8

    win10企业版安装 xmind.zip,打开应用程序报下面的错 解决办法是 先备份xmind8下的XMind.ini文件 Open up Xmind.ini and replace "Ap ...

  5. rake :You have already activated rake 10.1.0

    rake aborted! You have already activated rake 10.1.0, but your Gemfile requires rake 10.0.3. Using b ...

  6. [转载]jQuery1.9完全删除live事件

    其实关于live被删除的事件早就在1.7.2版本以后就有通知过了 不过在官方一直提倡使用.on代替.live的过程中 .live事件一直沿用 今天更新了 看下jQuery1.9的相关信息 发现api中 ...

  7. 如何使用openssl生成RSA公钥和私钥对

      在ubuntu上要使用openssl的话需要先进行安装,命令如下: sudo apt-get install openssl 安装完成就可以使用openssl了. 首先需要进入openssl的交互 ...

  8. [Linux] 无法访问国外网站,完成epel源安装的解决办法--待续

    一.缘由: 由于一个机房的网络限制,无法访问国外IP地址,在安装一些开源软件的时候比如smokeping.ansible就无法从epel源在线安装, 编译安装的话,又需要安装各种依赖,麻烦的一逼.所以 ...

  9. Tplink客户端设置

    之前在JD上面买了个Tplink,将公司的无线网转成有线的给我的台式机用,可是突然就掉线了,怎么配置都不行.甚至按向导去做了,后来连配置界面都进不去.然后又再某宝上面买了两个,回来配置也发现了这个情况 ...

  10. logcat--目录

    代码实现获取log日志和logcat使用方法:http://www.apkbus.com/android-128263-1-1.html logcat命令使用方法和查看android系统日志缓冲区内容 ...