直接用一个carry记录进位就可以

 1 //NEW
 2 class Solution {
 3     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 4         ListNode root = new ListNode(0);
 5         return addTwoNumbers(l1, l2, root);
 6     }
 7     public ListNode addTwoNumbers(ListNode l1, ListNode l2, ListNode root){
 8         int carry = 0;
 9         ListNode dummy = root;
10         while(l1 != null || l2!=null){
11             int x = (l1 != null) ? l1.val : 0;
12             int y = (l2 != null) ? l2.val : 0;
13             dummy.next = new ListNode((carry + x + y)%10);
14             dummy = dummy.next;
15             carry = (carry + x + y)/10;
16             if(l1!=null) {
17                 l1 = l1.next;
18             }
19             if(l2!=null) {
20                 l2 = l2.next;
21             }
22         }
23         if (carry > 0) {
24         dummy.next = new ListNode(carry);
25         }
26         return root.next;
27     }
28 }
29
30
31 //Old
32 class Solution {
33     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
34         String str1 = "", str2 = "";
35         while(l1 != null) {
36             str1 += Integer.toString(l1.val);
37             l1 = l1.next;
38         }
39         while(l2 != null) {
40             str2 += Integer.toString(l2.val);
41             l2 = l2.next;
42         }
43         char[] str11 = str1.toCharArray();
44         char[] str21 = str2.toCharArray();
45         char[] res = new char[Math.max(str11.length, str21.length) + 1];
46         for(int i = 0; i < res.length; i++) {
47             res[i] = 'a';
48         }
49         res[res.length - 1] = '0';
50         int record = 0;
51         for(int i = 0; i < res.length; i++) {
52             if(i < str11.length && i < str21.length) {
53                 if(str11[i]-'0' + str21[i]-'0' + record >= 10) {
54                     res[i] = (char)('0'+ ((str11[i]-'0') + (str21[i]-'0') +record) % 10);
55                     record = 1;
56                 }else {
57                     res[i] = (char)('0'+ (str11[i]-'0' + str21[i]-'0' +record));
58                     record = 0;
59                 }
60
61             }else if(i < str21.length) {
62                 if(str21[i]-'0' +record >= 10) {
63                     res[i] = (char)('0'+ (str21[i]-'0' +record) % 10);
64                     record = 1;
65                 }else {
66                     res[i] = (char)('0'+ (str21[i]-'0' +record));
67                     record = 0;
68                 }
69             }else if(i < str11.length){
70                 if(str11[i]-'0' +record >= 10) {
71                     res[i] = (char)('0'+ (str11[i]-'0' +record) % 10);
72                     record = 1;
73                 }else {
74                     res[i] = (char)('0'+ (str11[i]-'0' +record));
75                     record = 0;
76                 }
77             }else {
78                 if(record == 1) {
79                     res[i] = '1';
80                 }
81             }
82         }
83         ListNode node = new ListNode(res[0]-'0');
84         ListNode head = node;
85         for(int i = 1; i < res.length; i++) {
86             if(i != res.length - 1 || res[i] == '1') {
87                 node.next = new ListNode(res[i]-'0');
88                 node = node.next;
89             }
90
91         }
92         return head;
93     }
94 }

转载于:https://www.cnblogs.com/goPanama/p/9582440.html

2. Add Two Numbers相关推荐

  1. LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

    题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...

  2. [Leetcode] 445. Add Two Numbers II

    问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...

  3. LeetCode之Add Two Numbers

    LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...

  4. LeetCode刷题实战(2):Add Two Numbers

    题2描述: 2 Add Two Numbers 29.10% Medium You are given two non-empty linked lists representing two non- ...

  5. leetcode算法—两数相加 Add Two Numbers

    关注微信公众号:CodingTechWork,一起学习进步. 题目 Add Two Numbers: You are given two non-empty linked lists represen ...

  6. leetcode(二)—— Add Two Numbers(Python/C++)

    Add Two Numbers | LeetCode OJ 逻辑是简单的,主要是处理进位,以及两序列长度不一致的情况: Python # 链表结点定义 class ListNode(object):d ...

  7. LeetCode题解:Add Two Numbers

    LeetCode题解 说明:本人不是什么算法高手,部分内容参考了Google.stackoverflow.segmentfault中得到的一些解答或者代码.之所以去做Leetcode上的题是因为毕业工 ...

  8. LeetCode 445. Add Two Numbers II

    LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...

  9. 【注意】LeetCode 2. Add Two Numbers

    LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...

  10. LeetCode久不久来一题系列之Add Two Numbers

    题目来源: https://leetcode.com/problems/add-two-numbers/description/ 题目: Add Two Numbers 题目描述: You are g ...

最新文章

  1. 数据库连接池为什么要用threadlocal呢?不用会怎样?
  2. 字符流中第一个不重复的字符 python实现
  3. 最短路计数(spfa)
  4. C\C++对文件的读写操作
  5. Win7下VS2008升级补丁
  6. 使用Jenkins来实现内部的持续集成流程(下)
  7. 倍增:st表(模板)(洛谷P3865)
  8. CSS如何实现”右部宽度固定,左部自适应“的布局
  9. Oracle ADG备库SYSAUX数据文件坏块恢复处理(ORA-00600,ORA-10567,ORA-10564......
  10. ubuntu 14.04 linux下wifi驱动安装使用的一些笔记
  11. CentOS常用基础命令大全
  12. IPv6 to IPv4过渡技术——手工隧道和GRE隧道配置实例
  13. (1)Jenkins Linux环境下的简单搭建
  14. Python基础语法,基本数据类型及相关操作
  15. 【2017西安邀请赛:A】XOR(区间异或最大值多次查询---线段树+线性基合并)
  16. Unity3D脚本访问与参数传递
  17. 怎么批量修改html文件后缀,如何批量修改文件后缀名
  18. MATLAB数字图像小系统
  19. 免备案CDN免费 注册就送1T免费加速流量
  20. 周伟焜:杨元庆一定会带领联想成功

热门文章

  1. struts2官方 中文教程 系列六:表单验证
  2. description方法
  3. java正则表达式提取字符串中的数字
  4. iOS sqlite
  5. 短连接生成器——让你的url地址长度变短
  6. 数据结构03栈和队列
  7. OnLongClickListener长按事件设置墙纸
  8. 图灵社区 : 阅读 : 谁说Vim不是IDE?(三)
  9. 禁止ipc$默认共享的方法
  10. 数据清理最终实现了自动化