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 ...
随机推荐
- grep命令实例
grep一般用于查找文件中含有某些字符串的行,其命名格式如下 grep [OPTIONS] PATTERN [FILE...] 下面例举grep在linux使用过程中其常用使用实例: 1.grep递归 ...
- c++在函数后面加const
非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- .NET 面试题(1)
1.简述 private. protected. public. internal 修饰符的访问权限. private:私有成员,在类的内部才能访问 protected:保护成员,在该类内部和继承本类 ...
- Unity 屏幕适配小脚本
屏幕适配是可以通过代码实现的,相信给你时间就一定能写出来. 我们公司貌似没有分辨率适配框架通常对应小屏幕的苹果4要额外设置下等等就完了! 屏幕适配框架实现思路: 通过代码获取当前的分辨率 –> ...
- java与.net比较学习系列(5) 流程控制语句
java中流程控制语句主要分为以下几类,第一,条件语句,主要包括if语句和switch语句.第二,循环语句,主要包括while循环语句,for循环语句.第三,跳转语句,主要包括三种,break跳出语句 ...
- poj 1523 SPF(tarjan求割点)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- SQLLoader3(数据文件没有分隔符时的导入)
数据文件:D:\oracletest\ldr_tab_fiile.dat1.数据文件字段中间以制表符TAB隔开:7369 SMITH CLERK7499 ALLEN SALESMAN7521 WARD ...
- Unable to run mksdcard SDK tool.
Ubuntu 14.04,安装android studio后运行出错,sdk manager不能正常运行 Unable to run mksdcard SDK tool. 原因,缺少运行需要的库:li ...
- ajax参数中出现空格
jquery中发起ajax请求时的参数名中不能有空格.如果是get请求参数的中的空格会变成“+”符而在post请求中看不到这种变化,但无论哪种情况都无法与服务接口的参数就行匹配(此时进行调试也不会触发 ...