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. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  2. js 修改字符串中某些字符的样式

    var str = 'abcdefghijklmnobqrstuvwxyz'; function HightLight(e){ var reg = new RegExp(e, 'g') str = s ...

  3. leetcode-744-Find Smallest Letter Greater Than Target(改进的二分查找)

    题目描述: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...

  4. Springboot启动报Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered.

    解决方法: 属于bean重复,根据错误提示剔除多于的Bean引用!

  5. latex常用符号

    希腊字母 字母名称 大写 小写 大写latex 小写latex alpha A \(\alpha\) \alpha beta B \(\beta\) \beta gamma \(\Gamma\) \( ...

  6. js scroll nav

    http://jsfiddle.net/cse_tushar/Dxtyu/141/http://ironsummitmedia.github.io/startbootstrap-scrolling-n ...

  7. 20155213 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告

    20155213 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S ...

  8. HBase核心功能模块--读书笔记

    客户端Client 客户端 Client 是整个 HBase 系统的入口.使用者直接通过客户端操作 HBase.客户端 使用 HBase 的 RPC 机制与 HMaster 和 RegionServe ...

  9. Hadoop NameNode HA 和 ResourceManager HA

    1.集群规划 1.1 规划说明 hadoop1 cluster1 nameNode hadoop2 cluster1 nameNodeStandby ZooKeeper ResourceManager ...

  10. Maven学习(四)-----Maven中央存储库

    Maven中央存储库 当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如果没 ...