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. conda与pip的关系

    最近在搭建平台的时候,遇到了conda,具体尝试使用之后觉得conda还是非常好用的.他会交代清楚相互有依赖的包是什么,确认之后才会执行指令.要比pip好很多. 普通按照python 的时候一般都是自 ...

  2. 20145209刘一阳《网络对抗》Exp9 Web安全基础实践

    20145209刘一阳<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求 ...

  3. css实现div两列布局——左侧宽度固定,右侧宽度自适应(两种方法)

    原文:css实现div两列布局--左侧宽度固定,右侧宽度自适应(两种方法) 1.应用场景 左侧一个导航栏宽度固定,右侧内容根据用户浏览器窗口宽度进行自适应 2.思路 首先把这个问题分步解决,需要攻克以 ...

  4. SSM-CRUD实战

    前端最容易出现缓存问题,所以以后每次都必须完全在idea加载完后,再在浏览器端多 执行 ctrl+F5 索要最新copy 这样就能拿到最新的改动了,就不会出现各种代码没问题但是功能就是实现不了的问题 ...

  5. 如何写一个简单的HTTP服务器(重做版)

    最近几天用C++重新写了之前的HTTP服务器,对以前的代码进行改进.新的HTTP服务器采用Reactor模式,有多个线程并且每个线程有一个EventLoop,主程序将任务分发到每个线程,其中采用的是轮 ...

  6. 一个web应用的诞生(4)--数据存储

    上一章实现了登录的部分功能,之所以说是部分功能,是因为用户名和密码写成固定值肯定是不可以的,一个整体的功能,至少需要注册,登录,密码修改等,这就需要提供一个把这些值存储到数据库的能力. 当前的主流数据 ...

  7. python练习---小脚本

    一.爬子域名 #!/usr/bin/python # -*- coding: utf-8 -*- import requests import re import sys def get(domain ...

  8. Linux的常用命令笔记

    这里使用的是centos操作系统 一.简单命令 (1)查看历史纪录: history (2)查看当前目录: pwd (3)查看系统当前时间和日期 date (4)查看当前登陆到系统的所有用户 who ...

  9. Spring Cloud(一):服务治理技术概览【Finchley 版】

    Spring Cloud(一):服务治理技术概览[Finchley 版]  发表于 2018-04-14 |  更新于 2018-05-07 |  Spring Cloud Netflix 是 Spr ...

  10. 【python 3.6】调用另一个文件的类的方法

    文件1:test12.py 文件2:test13.py 文件1 如下: #!/usr/bin/python # -*- coding: utf-8 -*- ''' ''' class abcd(obj ...