2.5 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.
EXAMPLE
Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.

LeetCode上的原题,请参见我之前的博客Add Two Numbers 两个数字相加

跟那道LeetCode有所不同的是,这道题还有个Follow Up,把链表存的数字方向变了,原来是表头存最低位,现在是表头存最高位。既然是翻转了链表,那么一种直接的解法是把两个输入链表都各自翻转一下,然后用之前的方法相加完成,再把得到的结果翻转一次,就是结果了,翻转链表的方法可以参见我之前的博客Reverse Linked List 倒置链表。代码如下:

解法一:

  1. // Follow up
  2. class Solution {
  3. public:
  4. ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
  5. ListNode *dummy = new ListNode(-);
  6. ListNode *cur = dummy;
  7. int carry = ;
  8. l1 = reverseList(l1);
  9. l2 = reverseList(l2);
  10. while (l1 || l2) {
  11. int n1 = l1 ? l1->val : ;
  12. int n2 = l2 ? l2->val : ;
  13. int sum = n1 + n2 + carry;
  14. carry = sum / ;
  15. cur->next = new ListNode(sum % );
  16. cur = cur->next;
  17. if (l1) l1 = l1->next;
  18. if (l2) l2 = l2->next;
  19. }
  20. if (carry) cur->next = new ListNode();
  21. return reverseList(dummy->next);
  22. }
  23. ListNode *reverseList(ListNode *head) {
  24. if (!head) return head;
  25. ListNode *dummy = new ListNode(-);
  26. dummy->next = head;
  27. ListNode *cur = head;
  28. while (cur->next) {
  29. ListNode *tmp = cur->next;
  30. cur->next = tmp->next;
  31. tmp->next = dummy->next;
  32. dummy->next = tmp;
  33. }
  34. return dummy->next;
  35. }
  36. };

如果我们不采用翻转链表的方法该怎么做呢,这就比较复杂了。首先我们要县分别计算出两个链表的长度,然后给稍短一点的链表前面补0,补到和另一个链表相同的长度。由于要从低位开始相加,而低位是链表的末尾,所以我们采用递归来处理,先遍历到链表的末尾,然后从后面相加,进位标示符carry用的是引用,这样保证了再递归回溯时值可以正确传递,每次计算的节点后面接上上一次回溯的节点,直到回到首节点完成递归。最后还是处理最高位的进位问题。代码如下:

解法二:

  1. // Follow up
  2. class Solution {
  3. public:
  4. ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
  5. int n1 = , n2 = , carry = ;;
  6. n1 = getLength(l1);
  7. n2 = getLength(l2);
  8. if (n1 > n2) l2 = padList(l2, n1 - n2);
  9. if (n2 > n1) l1 = padList(l1, n2 - n1);
  10. ListNode *res = addTwoNumbersDFS(l1, l2, carry);
  11. if (carry == ) {
  12. ListNode *tmp = new ListNode();
  13. tmp->next = res;
  14. res = tmp;
  15. }
  16. return res;
  17. }
  18. ListNode *addTwoNumbersDFS(ListNode *l1, ListNode *l2, int &carry) {
  19. if (!l1 && !l2) return NULL;
  20. ListNode *list = addTwoNumbersDFS(l1->next, l2->next, carry);
  21. int sum = l1->val + l2->val + carry;
  22. ListNode *res = new ListNode(sum % );
  23. res->next = list;
  24. carry = sum / ;
  25. return res;
  26. }
  27. ListNode *padList(ListNode *list, int len) {
  28. ListNode *dummy = new ListNode(-);
  29. ListNode *cur = dummy;
  30. for (int i = ; i < len; ++i) {
  31. cur->next = new ListNode();
  32. cur = cur->next;
  33. }
  34. cur->next = list;
  35. return dummy->next;
  36. }
  37. int getLength(ListNode *list) {
  38. ListNode *cur = list;
  39. int res = ;
  40. while (cur) {
  41. ++res;
  42. cur = cur->next;
  43. }
  44. return res;
  45. }
  46. };

[CareerCup] 2.5 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. [LeetCode] 2. Add Two Numbers 两个数字相加

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

  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. [LintCode] Add Two Numbers 两个数字相加

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. web项目知识整理

    一.div居中 1.margin:auto 2.left:50%:margin-left:div宽度的一半 二.一般处理程序操作session 引using System.Web.SessionSta ...

  2. jQuery静态方法noConflict的使用和源码分析

    所谓静态方法是jQuery本身得公共方法,并不需要通过实例化来调用,一般也称为工具方法,下面先来列绝下jQuery.noConflict方法的用法: noConflict() 方法让渡变量 $ 的 j ...

  3. AngularJS在IE8的支持

    AngularJS一般不会选择IE8支持, 因为很多特性在IE8下效果很差, 性能也不好, 但是由于项目的需要, 客户的机器有些是XP, 只能够装IE8, 所以为了解决这个, 我查阅了相关的资料,发现 ...

  4. iOS UIMenuController菜单

    //1:普通 ////  ViewController.m//  DemoTest#import "ViewController.h"@interface ViewControll ...

  5. Xcode 创建.a和framework静态库(转载)

    库介绍 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种. iOS中的静态库有 .a 和 .framework两种形式:动态库有.dylib 和 .framew ...

  6. 教你开发asp.net的单点登录系统

    单点登录系统,简称SSO.以下是我花了几个小时写的一个简单实现.特把实现思路和大家分享. 背景:某项目使用ASP.NET MemberShip来做会员系统,需要同时登录多个系统.而项目的开发人员无法在 ...

  7. web.xml中监听器配置

    <!-- 监听器的配置:监听器配置完以后,应用系统在启动的时候就会开启这些监听器. 监听器的理解:监听器好比一个卫兵,卫兵一直站在那里等待长官的命令,当卫兵收到长官的命令以后,立即执行 之前已经 ...

  8. Mysql存储过程和函数区别介绍

    存储过程是用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返回某种类型的值并且不涉及特定用户表. 存储过程和函数存在以下 ...

  9. 特定IP访问远程桌面

    1. 新建IP安全策略 (远程端口没有修改情况下的设置) WIN+R打开运行对话框,输入“gpedit.msc”进入组策略编辑器. 依次打开:本地计算机策略--计算机配置--Windows设置--安全 ...

  10. 某中国500强企业BI系统成功应用案例

    随着某集团20多年的不断发展发展,现已成为中国500强.中国大企业集团竞争力前25强.中国信息化标杆企业和国家重点火炬高新技术企业.拥有总资产数十亿元.员工数万名,涉足电力.家电.能源.等多个行业,并 ...