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 ...
随机推荐
- 解决Struts2.2.20版本的标签不支持style属性的问题
我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, ...
- July 【补题】
A(zoj 3596) bfs,记忆搜都可以, 按余数来记录状态. B(zoj 3599) 博弈,跳过 C(zoj 3592) 简单dp,题意不好懂 D(zoj 3602) 子树哈希, 对根的左右儿子 ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- python学习之路-11 多线程、多进程、协程
python内置队列模块 queue queue的四种队列 q = queue.Queue() # 先进先出队列 q = queue.LifoQueue() # 后进先出队列 q = queue.Pr ...
- [Java 并发] Java并发编程实践 思维导图 - 第一章 简单介绍
阅读<Java并发编程实践>一书后整理的思维导图.
- hadoop备战:一台x86计算机搭建hadoop的全分布式集群
主要的软硬件配置: x86台式机,window7 64位系统 vb虚拟机(x86的台式机至少是4G内存,才干开3台虚机) centos6.4操作系统 hadoop-1.1.2.tar.gz jdk- ...
- CLR via C# - 基础拾遗
编译器开关设置 IL代码质量 JIT本地代码质量 /optimize- /debug-(默认设置) 未优化 优化 /optimize- /debug+(full/pdbonly) 未优化 未优化 /o ...
- const用法总结
1. const修饰变量 ; const int* a = &b; //情况1 int const* a = &b; //情况2 int* const a = &b; //情况 ...
- SQL SERVER 清空日志
DUMP TRANSACTION [TBNAME] WITH NO_LOGBACKUP LOG [TBNAME] WITH NO_LOGDBCC SHRINKDATABASE([TBNAME]) 1. ...
- 写一个Windows上的守护进程(4)日志其余
写一个Windows上的守护进程(4)日志其余 这次把和日志相关的其他东西一并说了. 一.vaformat C++日志接口通常有两种形式:流输入形式,printf形式. 我采用printf形式,因为流 ...