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

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.

题意

你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。

解法一:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode addLists(ListNode l1, ListNode l2) {
  14. if(l1 == null && l2 == null) {
  15. return null;
  16. }
  17.  
  18. ListNode head = new ListNode(0);
  19. ListNode point = head;
  20. int carry = 0;
  21. while(l1 != null && l2!=null){
  22. int sum = carry + l1.val + l2.val;
  23. point.next = new ListNode(sum % 10);
  24. carry = sum / 10;
  25. l1 = l1.next;
  26. l2 = l2.next;
  27. point = point.next;
  28. }
  29.  
  30. while(l1 != null) {
  31. int sum = carry + l1.val;
  32. point.next = new ListNode(sum % 10);
  33. carry = sum /10;
  34. l1 = l1.next;
  35. point = point.next;
  36. }
  37.  
  38. while(l2 != null) {
  39. int sum = carry + l2.val;
  40. point.next = new ListNode(sum % 10);
  41. carry = sum /10;
  42. l2 = l2.next;
  43. point = point.next;
  44. }
  45.  
  46. if (carry != 0) {
  47. point.next = new ListNode(carry);
  48. }
  49. return head.next;
  50. }
  51. }

中规中矩的解法

解法二:

  1. public class Solution {
  2. /**
  3. * @param l1: the first list
  4. * @param l2: the second list
  5. * @return: the sum list of l1 and l2
  6. */
  7. public ListNode addLists(ListNode l1, ListNode l2) {
  8. ListNode dummy = new ListNode(0);
  9. ListNode tail = dummy;
  10.  
  11. int carry = 0;
  12. for (ListNode i = l1, j = l2; i != null || j != null; ) {
  13. int sum = carry;
  14. sum += (i != null) ? i.val : 0;
  15. sum += (j != null) ? j.val : 0;
  16.  
  17. tail.next = new ListNode(sum % 10);
  18. tail = tail.next;
  19.  
  20. carry = sum / 10;
  21. i = (i == null) ? i : i.next;
  22. j = (j == null) ? j : j.next;
  23. }
  24.  
  25. if (carry != 0) {
  26. tail.next = new ListNode(carry);
  27. }
  28. return dummy.next;
  29. }
  30. }

比较简明的写法,且使用了dummy节点,参考@NineChapter 的代码

解法三:

  1. public class Solution {
  2. /*
  3. * @param l1: the first list
  4. * @param l2: the second list
  5. * @return: the sum list of l1 and l2
  6. */
  7. public ListNode addLists(ListNode l1, ListNode l2) {
  8. ListNode root = new ListNode(-1);
  9. ListNode result = root;
  10. int carry = 0;
  11.  
  12. while( l1 != null || l2 != null || carry == 1){
  13. int value = 0;
  14. if(l1 != null){
  15. value += l1.val;
  16. l1 = l1.next;
  17. }
  18. if( l2 != null){
  19. value += l2.val;
  20. l2 = l2.next;
  21. }
  22.  
  23. value += carry;
  24. root.next = new ListNode(value % 10);
  25. carry = value / 10;
  26.  
  27. root = root.next;
  28.  
  29. }
  30.  
  31. return result.next;
  32. }
  33. }

解法四:

  1. class Solution {
  2. public:
  3. ListNode* addLists(ListNode* l1, ListNode* l2) {
  4. ListNode *head = new ListNode();
  5. ListNode *ptr = head;
  6. int carry = ;
  7. while (true) {
  8. if (l1 != NULL) {
  9. carry += l1->val;
  10. l1 = l1->next;
  11. }
  12. if (l2 != NULL) {
  13. carry += l2->val;
  14. l2 = l2->next;
  15. }
  16. ptr->val = carry % ;
  17. carry /= ;
  18. // 当两个表非空或者仍有进位时需要继续运算,否则退出循环
  19. if (l1 != NULL || l2 != NULL || carry != ) {
  20. ptr = (ptr->next = new ListNode());
  21. } else {
  22. break;
  23. }
  24. }
  25. return head;
  26. }
  27. };

非常简明的代码,参考@NineChapter 的代码

167. Add Two Numbers【easy】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 167. Add Two Numbers【LintCode by java】

    Description You have two numbers represented by a linked list, where each node contains a single dig ...

  3. 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】

    Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...

  4. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  5. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  6. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  7. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  8. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  9. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

随机推荐

  1. [Android Pro] android 杀死进程的方法

    1: 杀死自己进程的方法 android.os.Process.killProcess(Process.myPid()); 2:杀死别人进程的方法(不能杀死自己) -------a: activity ...

  2. vc预处理

    VC 编译命令开关 vc可以可以通过Settings -->Project-->C/C++-->Customize来设置这个编译开关 /C:在预处理输出中保留注释语句 /c:只编译, ...

  3. 用指令来构建IIS7

    工作上要部署iis7+net4.0环境,发现30多台机子都没有用装有IIS7镜像来安装,都必须自己手动. 作为程序猿,真要一台台装的话,就真对不起自己的职业.于是想到用bat来执行任务,找到了安装II ...

  4. Qt 维护工具MaintenanceTool.exe 使用

    QT如何管理组件(解决“要继续此操作,至少需要一个有效且已启用的储存库”问题) 转载2017-10-26 01:48:46 标签:qt QT的组件管理软件并没有在开始菜单或者桌面添加快捷方式(5.9版 ...

  5. 《java 语言程序设计》第2章编程练习

    2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System. ...

  6. phpstorm failed to create jvm:error code -6 解决办法 解决方法

    phpStorm 软件打开运行提示 failed to create JVM的解决办法. 修改文件 D:\Program Files (x86)\JetBrains\PhpStorm 7.1.3\bi ...

  7. [NPM] Avoid Duplicate Commands by Calling one NPM Script from Another

    We can get a lot of utility through CLI tools invoked via npm scripts. Many of these tools have APIs ...

  8. 让Android App启动更协调

          不知道大伙有没有发现,应用第一次启动的时候一般比较慢(低配置手机尤其如此),黑屏好一段时间,下面是我在模拟器中启动QQ的截图,黑屏差不多有5秒左右,如下图所示~      显然这种结果很糟 ...

  9. keytool命令总结

    keytool 命令总结 一.创建数字证书 交互模式 使用默认的密钥库.keystore(目录是c: Documents and Setting用户名)和算法(DSA) keytool -genkey ...

  10. 机器学习数学基础- gradient descent算法(上)

    为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...