题目

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. 【NOIP2009 T3】 最佳贸易 (双向SPFA)

    C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...

  2. wordpress(三)wordpress手动更新

    第一:备份数据库还有文件 第二:从WP中文官网下载最新版WordPress,下载完毕解压到你电脑上. 第三:删除博客主机上的wp-includes和wp-admin目录. 第四:将解压在本地电脑的wo ...

  3. tar命令

    # tar -cvf /usr/local/auto_bak/test.tar /usr/local/test 仅打包,不压缩 # tar -zcvf /usr/local/auto_bak/test ...

  4. js生成二维码(jquery自带)

    //引入js<script type="text/javascript" src="js/jquery.js"></script> &l ...

  5. 044. asp.net主题之三应用或禁用主题和动态加载主题

    1.为单个页面指定主题可以将@Page指令的Theme或StyleSheetTheme属性设置为要使用的主题名称, 代码如下: <%@ Page Theme ="MyTheme&quo ...

  6. install Hadoop

    Installing Java Hadoop runs on both Unix and Windows operating systems, and requires Java to beinsta ...

  7. 调试腾讯微博 win8 版 共享失败的问题

    我是社交控,喜欢分享内容.分享到 腾讯微博时总失败,心想不能就这么算了,要看看异常的细节. 在VS 2012里,我选择 Debug > Debug Installed App Package, ...

  8. Microsoft Win32 to Microsoft .NET Framework API Map

    Microsoft Win32 to Microsoft .NET Framework API Map .NET Development (General) Technical Articles   ...

  9. 高通AR增强现实Unity3D

    AR: 增强现实,台湾翻译叫做扩张实境 1.注册.然后下载sdk(注册账号主要是为了第3步中制作识别图而用的) 下载地址:https://developer.vuforia.com/resources ...

  10. GitBook制作电子书详细教程(命令行版)

    GitBook 是一款基于 Node.js 开发的开源的工具,可以通过命令行的方式创建电子书项目,再使用 MarkDown 编写电子书内容,然后生成 PDF.ePub.mobi 格式的电子书,或生成一 ...