You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

Tips:给定两个单链表,将两个单链表中的值相加。

思路:链表都是按照从前到后连接的,而整数加法需要从低位开始相加,即链表的后面开始相加。

我才用两个list来保存链表的val,按照从后向前的顺序相加,并将结果保存在一个list中。最后将list按照从后向前的顺序,赋值给一个新的链表。

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
while (l1 != null) {
list1.add(l1.val);
l1 = l1.next;
}
while (l2 != null) {
list2.add(l2.val);
l2 = l2.next;
}
int len1 = list1.size();
int len2 = list2.size();
//将较长的链表保存在list1中
if (len1 < len2) {
List<Integer> temp = list2;
list2 = list1;
list1 = temp;
}
int len = len1 > len2 ? len1 : len2;// 最后链表的长度为len或者len+1
System.out.println(len);
List<Integer> list = new ArrayList<>();
int diff = Math.abs(len1 - len2);
int tag = 0;
//从后向前进行整数加法。
for (int i = len - 1; i >= 0; i--) {
//如果两个链表长度不相等,保证右对齐 ,先将能对其的位置相加.剩下的位置只加list1即可
int temp = tag + list1.get(i);
if (i - diff >= 0) {
temp += list2.get(i - diff);
}
if (temp >= 10) {
//进1
tag = 1;
temp -= 10;
} else {
tag = 0;
}
list.add(temp);
}
if (tag > 0)
list.add(1);
//打印list中的值
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
ListNode head = new ListNode(-1);
ListNode node = new ListNode(0);
head.next = node;
//将list从后向前 赋值给链表结点
for (int i = list.size() - 1; i >= 0; i--) {
node.next = new ListNode(list.get(i));
node = node.next;
}
node.next = null;
return head.next.next;
}

测试代码:

public static void main(String[] args) {
ListNode node1 = new ListNode(7);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(4);
ListNode node4 = new ListNode(3);
node1.next = node2;
node2.next = node3;
node3.next = node4;
ListNode nu = null;
node4.next = nu; ListNode node11 = new ListNode(5);
ListNode node21 = new ListNode(6);
ListNode node31 = new ListNode(4);
node11.next = node21;
node21.next = node31;
ListNode nu1 = null;
node31.next = nu1;
L445AddTwoNumbersII l445 = new L445AddTwoNumbersII();
ListNode head = l445.addTwoNumbers(node11, node1);
while (head != null) {
System.out.println("~~~~~~~~~~~~");
System.out.println(head.val);
head = head.next;
}
}

【Leetcode】445. Add Two Numbers II的更多相关文章

  1. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  2. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  3. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  4. 【LeetCode】2.Add Two Numbers

    首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长. struct ListNode* addTwoNumbers(struct ListNode* l ...

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

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

  6. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  8. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  9. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. UVA 514 - Rails ( 铁轨)

    from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...

  2. [Err] ERROR: wrong record type supplied in RETURN NEXT

    在写GP 输出不定长列数据表 函数时,报了一个错,百思不得其解.在公司大佬帮助下,知道是什么鬼了.. 先看看例子吧: ---- 函数定义 CREATE OR REPLACE FUNCTION &quo ...

  3. Asp.Net实现在线人数统计 (转)

    原文件:http://blog.csdn.net/wxd_860825/article/details/4589292 利用Application对象和Session对象可以统计当前在线用户数量. 注 ...

  4. PL/SQL轻量版(四)——存储函数/存储过程与触发器

    概述 ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数.过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块,均存储在数 ...

  5. SPOJ 694&&SPOJ705: Distinct Substrings

    DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...

  6. git在windows7下面使用

    1. 首选安装. 2. 打开Git Bash 3. 输入,就是配置一下用户名啥的 $ git config --global user.name "Jack Liao" $ git ...

  7. javaweb(三十八)——mysql事务和锁InnoDB(扩展)

    MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备就My ...

  8. linux安装PHP-memcache-redis扩展

    1.php memcache 扩展 http://pecl.php.net/package/memcache/3.0.8 下载文件源码 #tar zxvf memcache-3.0.8.tar#/us ...

  9. android 图片二维码识别和保存(一)

    最新业务开发二维码识别的功能,这个功能,在很多应用上都有,比如微信长按图片识别二维码,如果图片中存在可以识别的二维码时,可以增加一个选项 识别二维码.那么如何去实现这个功能呢.这里其实也非常简单,首先 ...

  10. html5新特性localStorage和sessionStorage

    HTML5 提供了两种在客户端存储数据的新方法: localStorage: (1)它的生命周期是永久的,关闭页面或浏览器之后localStorage中的数据也不会消失. (2)它的容量大小是5M作用 ...