题目

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. ExtJs 学习之开篇(三)Ext.grid.Panel表格中的处理

    Ext.grid.Panel Ext.create('Ext.grid.Panel',{        title:'测试表格',        width:400,        height:20 ...

  2. ChartDirector 6.0在MFC下乱码问题

    XYChart *c = new XYChart(640, 350); char buffer1[256] = ""; UnicodeToUtf8(L"Realtime ...

  3. Python>>>The Very First Step

    Windows官网下载 python-2.7.13.amd64.msi 默认会安装pip,同是把设置环境变量也选中 安装第三方包: pip install pygame 确认已经正确安装的方法:1.完 ...

  4. svn import后,服务器上少了所有*.a文件的问题解决

    转载自:http://blog.csdn.net/lwl_ls/article/details/20222051 将本地代码import到svn服务器. svn co出代码,编译却报错少了这个那个*. ...

  5. 【转】JSch - Java实现的SFTP(文件下载详解篇)

    上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等.   同样,JSch的文件下载也支持三种传输模式:OVERWRI ...

  6. android APK 文件的生成过程

    步骤: 1. 用 aapt工具生成R文件aapt  package  -m -J  gen目录 -M AndroidManifest.xml  -S res目录  -I 编译版本sdk的android ...

  7. 学习SQL的点点滴滴(四)-UPDATE小计

    1.更新tb_card中c_customer字段的值等于tb_customer表中c_no的值 update tb_card set c_customer=ct.c_no from tb_custom ...

  8. log4net 配置

    1.是直接在代码中通过调用XmlConfigurator.Configure()来解析配置文件,配置日志环境. log4net.Config.XmlConfigurator.Configure(); ...

  9. OAF_开发系列18_实现OAF页面跳转setForwardURL / forwardImmediately(案例)

    20150716 Created By BaoXinjian

  10. Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)

    C. Alyona and mex Problem Description: Alyona's mother wants to present an array of n non-negative i ...