cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum
2.5 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 Input: (3 -> 1 -> 5) + (5 -> 9 -> 2) Output: 8 -> 0 -> 8
给两个整型链表,每个节点包含一个位数,这些位数是反向存放的,也就是个位数在链表首部,编写程序,对这两个整数求和,并用链表形式返回结果。
进阶:
假设这些位数是正向存放的,请再做一遍。
1.自己的思路是:取每个list的第一个,相加(随时取余,相除),放入新建一个列表的最后一个。
public class newAdd5 {
public static LinkedList<Integer> sum1(LinkedList<Integer> l1,
LinkedList<Integer> l2) {
LinkedList<Integer> l3 = new LinkedList<Integer>();
if (l1.isEmpty())
return l2;
if (l2.isEmpty())
return l1;
int carry = 0;
while (!l1.isEmpty() && !l2.isEmpty()) {
int sum = 0;
sum = (l1.peek() + l2.peek() + carry) % 10;
carry = (l1.poll() + l2.poll() + carry) / 10;
l3.addFirst(sum);
}
while (!l1.isEmpty() && l2.isEmpty()) {
int sum = 0;
sum = (l1.peek() + carry) % 10;
carry = (l1.poll() + carry) / 10;
l3.addFirst(sum);
}
while (!l2.isEmpty() && l1.isEmpty()) {
int sum = 0;
sum = (l2.peek() + carry) % 10;
carry = (l2.poll() + carry) / 10;
l3.addFirst(sum);
}
return l3;
}
// follow up
public static LinkedList<Integer> sum2(LinkedList<Integer> l1,
LinkedList<Integer> l2) {
java.util.Collections.reverse(l1);
java.util.Collections.reverse(l2);
LinkedList<Integer> l3 = new LinkedList<Integer>();
if (l1.isEmpty())
return l2;
if (l2.isEmpty())
return l1;
int carry = 0;
while (!l1.isEmpty() && !l2.isEmpty()) {
int sum = 0;
sum = (l1.peek() + l2.peek() + carry) % 10;
carry = (l1.poll() + l2.poll() + carry) / 10;
l3.addFirst(sum);
}
while (!l1.isEmpty() && l2.isEmpty()) {
int sum = 0;
sum = (l1.peek() + carry) % 10;
carry = (l1.poll() + carry) / 10;
l3.addFirst(sum);
}
while (!l2.isEmpty() && l1.isEmpty()) {
int sum = 0;
sum = (l2.peek() + carry) % 10;
carry = (l2.poll() + carry) / 10;
l3.addFirst(sum);
}
return l3;
}
public static void main(String[] args) {
Random rd = new Random();
LinkedList<Integer> list1 = new LinkedList<Integer>();
LinkedList<Integer> list2 = new LinkedList<Integer>();
int n1 = rd.nextInt(5);
for (int i = 0; i < n1; i++) {
list1.add(rd.nextInt(10));
}
int n2 = rd.nextInt(6);
for (int i = 0; i < n2; i++) {
list2.add(rd.nextInt(10));
}
System.out.println(list1.toString());
System.out.println(list2.toString());
System.out.println("After Adding: ");
System.out.println(sum1(list1, list2).toString());
LinkedList<Integer> list3 = new LinkedList<Integer>();
LinkedList<Integer> list4 = new LinkedList<Integer>();
for (int i = 0; i < n1; i++) {
list3.add(rd.nextInt(10));
}
for (int i = 0; i < n2; i++) {
list4.add(rd.nextInt(10));
}
System.out.println("Follow Up: ");
System.out.println(list3.toString());
System.out.println(list4.toString());
System.out.println(sum2(list3, list4).toString());
}
}
然后在网上看的方法更简洁:
public class Sum5 {
public static void main(String[] args) {
Random rd = new Random();
LinkedList<Integer> list1 = new LinkedList<Integer>();
LinkedList<Integer> list2 = new LinkedList<Integer>();
int n1 = rd.nextInt(5);
for (int i = 0; i < n1; i++) {
list1.add(rd.nextInt(10));
}
int n2 = rd.nextInt(6);
for (int i = 0; i < n2; i++) {
list2.add(rd.nextInt(10));
}
System.out.println(list1.toString());
System.out.println(list2.toString());
System.out.println("After Adding: ");
System.out.println(addReverse(list1, list2).toString());
LinkedList<Integer> list3 = new LinkedList<Integer>();
LinkedList<Integer> list4 = new LinkedList<Integer>();
for (int i = 0; i < n1; i++) {
list3.add(rd.nextInt(10));
}
for (int i = 0; i < n2; i++) {
list4.add(rd.nextInt(10));
}
System.out.println("Follow Up: ");
System.out.println(list3.toString());
System.out.println(list4.toString());
System.out.println(addForward(list3, list4).toString());
}
private static LinkedList<Integer> addReverse(LinkedList<Integer> list1,
LinkedList<Integer> list2) {
LinkedList<Integer> list3 = new LinkedList<Integer>();
int sum = 0;
while (!list1.isEmpty() || !list2.isEmpty() || sum != 0) {
int tempsum = sum;
if (!list1.isEmpty()) {
tempsum += list1.poll();
}
if (!list2.isEmpty()) {
tempsum += list2.poll();
}
list3.addFirst(tempsum % 10);
sum = tempsum / 10;
}
return list3;
}
private static LinkedList<Integer> addForward(LinkedList<Integer> list1,
LinkedList<Integer> list2) {
LinkedList<Integer> list3 = new LinkedList<Integer>();
int sum = 0;
java.util.Collections.reverse(list1);
java.util.Collections.reverse(list2);
while (!list1.isEmpty() || !list2.isEmpty() || sum != 0) {
int tempsum = sum;
if (!list1.isEmpty()) {
tempsum += list1.poll();
}
if (!list2.isEmpty()) {
tempsum += list2.poll();
}
list3.addFirst(tempsum % 10);
sum = tempsum / 10;
}
return list3;
}
}
cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum的更多相关文章
- cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- LeetCode Linked List Medium 2. Add Two Numbers
Description You are given two non-empty linked lists representing two non-negative integers. The dig ...
- C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Leetcode 160. Intersection of two linked lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- RCU 机制 [转IBM]
2005 年 7 月 01 日 本文详细地介绍了 Linux 2.6 内核中新的锁机制 RCU(Read-Copy Update) 的实现机制,使用要求与典型应用. 一.引言 众所周知,为了保护共享数 ...
- 第二章 Android Studio使用第三方模拟器
1.为什么要使用第三方模拟器 Android Studio自带模拟器,相对Eclipse来说项目启动速度的确快了很多倍,提高了开发效率.但和第三方模拟器进行对比的话,还是第三方的模拟器运行速度更快些. ...
- GridView+ZedGraph【转】
edgraph图表控件的强大功能令人出乎意料,与OWC相比我想应该毫不逊色,近来需求要求作出相关数据统计,不想使用BI这类的强大东西,所以搜索到 了免费的开源的Zedgraph控件.使用起来也非常方便 ...
- size_t和size_type类型
size_t一般用来表示一种计数,比如有多少东西被拷贝等.例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小. 它的意义大致是“适于计量内存中可容纳的数据 ...
- (转)Should I use char** argv or char* argv[]
As you are just learning C, i recommend you to really try to understand the differences between ar ...
- (原)ubuntu上安装nvidia及torch的nccl
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5717234.html 参考网址: https://github.com/NVIDIA/nccl htt ...
- Memcache入门知识
Memcache适合做缓存,是一款管理内存的很小的软件,实现对内存数据的管理,一般我们用memcache存储临时数据,因为内存不能储存永久化的数据,内存里面的数据,断电就消失了. memcache可以 ...
- c++连接mysql数据库(使用mysql api方式,环境VS2013+MYSQL5.6)
转载请注明出处,原文地址http://www.cnblogs.com/zenki-kong/p/4382657.html 刚开始写博客,博主还只是个大三汪,学艺不精,如有错误还请前辈指出(>^ω ...
- node.js 环境搭建
一 官网下载安装包 : 1.http://www.nodejs.org/download/ 选择相应的包进行安装 2.安装express : npm install -g express -gener ...
- PROCEDURE_监测系统_告警信息存储过程—产生告警信息插入告警表
create or replace procedure proc_alarmlog(in_id in number, --采集器编码 ...