You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Have you met this question in a real interview?

Yes
Example

Given 7->1->6 + 5->9->2. That is, 617 + 295.

Return 2->1->9. That is 912.

Given 3->1->5 and 5->9->2, return 8->0->8.

LeetCode上的原题,请参见我之前的博客Add Two Numbers

  1. class Solution {
  2. public:
  3. /**
  4. * @param l1: the first list
  5. * @param l2: the second list
  6. * @return: the sum list of l1 and l2
  7. */
  8. ListNode *addLists(ListNode *l1, ListNode *l2) {
  9. ListNode *dummy = new ListNode(-), *cur = dummy;
  10. int carry = ;
  11. while (l1 || l2) {
  12. int num1 = l1 ? l1->val : ;
  13. int num2 = l2 ? l2->val : ;
  14. int sum = num1 + num2 + carry;
  15. cur->next = new ListNode(sum % );
  16. carry = sum / ;
  17. if (l1) l1 = l1->next;
  18. if (l2) l2 = l2->next;
  19. cur = cur->next;
  20. }
  21. if (carry) cur->next = new ListNode();
  22. return dummy->next;
  23. }
  24. };

[LintCode] Add Two Numbers 两个数字相加的更多相关文章

  1. [LeetCode] Add Two Numbers 两个数字相加

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

  2. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

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

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

  4. [LeetCode] 2. Add Two Numbers 两个数字相加

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

  5. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  6. LeetCode 2. Add Two Numbers (两数相加)

    题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...

  7. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  8. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  9. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

随机推荐

  1. java生产者/消费者模式实现——一生产者一消费者(操作值)

    胶多不粘话多不甜,直接上代码: 生产者类: /** * Created by 51304 on 2016/2/28. */ public class P { private String lock; ...

  2. 机器学习 k-临近算法

    程序清单一: from numpy import * import operator def creatDataSet(): group = array([[1.0,1.1],[1.0,1.0],[0 ...

  3. Android--UI之Spinner

    前言 最近一直在讲AndroidUI的开发,今天讲一下Spinner控件,这是一个列表选择框,可以弹出一个列表供用户选择.在本片博客中,会讲解Spinner的基本属性以及设置之后的效果,以及使用Sim ...

  4. linux Mint截图软件 Shutter

    安装shutter: sudo add-apt-repository ppa:shutter/ppa sudo apt-get update sudo apt-get install shutter ...

  5. iOS 向客户发送xcarchive文件(整理中)

    由于安全的原因,客户不会把app的 distribution provisioning profile给开发者的,但是可以给一个development provisioning profile.这种情 ...

  6. 【安装MongoDB】CentOS7 下安装NodeJs+Express+MongoDB+Redis

    MongoDB,V3.2版本,官网说的很详细,见链接:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 1.创建 ...

  7. Ubuntu 14.4 配置

    1.安装 Orcal Java 使用下面的命令安装,只需一些时间,它就会下载许多的文件,所及你要确保你的网络环境良好: sudo add-apt-repository ppa:webupd8team/ ...

  8. 使用微信web开发者工具调试微信企业号页面(前端页面,已发布在服务器上的)

    前几天写了一篇使用fiddler调试微信端页面的,然后博友评论说使用fiddler太麻烦了,推荐使用微信web开发者工具调试微信页面,这两天弄着玩了一下,很强大.这篇文章只是做一个记录,方便自己以后使 ...

  9. JS 中没有按地址(引用)传递,只有按值传递

    很多人,包括我,受书本知识消化不彻底的影响,认为 JS 中参数有两种传递方式:数字.字符串等按值传递:数组.对象等按地址(引用)传递.对此种观点,我们要谨慎. var v1 = [] var v2 = ...

  10. C++ 的二进制语法与语义

    /* 转载请注明出处:http://www.cnblogs.com/Martinium/p/binary_literal.html */ 二进制的语法 C/C++ 默认数字使用十进制,八进制使用前缀 ...