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的更多相关文章

  1. 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 ...

  2. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  3. LeetCode Linked List Medium 2. Add Two Numbers

    Description You are given two non-empty linked lists representing two non-negative integers. The dig ...

  4. C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...

  5. [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 ...

  6. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  7. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  8. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. 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 ...

随机推荐

  1. UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>

    J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  2. 本地plsqldev.exe连接远端oracle数据库

    先看百度经验:http://jingyan.baidu.com/article/48b558e3540ecf7f38c09a3c.html 这里如果我们只有安装plsql工具,下载oracle精简版本 ...

  3. WPF动画

    System.Windows.Media.Animation 这个命名空间中,包含了三种动画类:线性插值动画类(17个).关键帧动画(22个).路径动画(3个). 线性插值动画类(17个)如下: By ...

  4. mysql 基础技术

    一.树状结构 参考http://www.cnblogs.com/kingteach/archive/2011/07/05/2098046.html )) begin declare lev int; ...

  5. KVO奥秘

    序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣的回调机制,在某个对象注册监听者后,在被监听的对象发生改变时,对象会发送一个通知给 ...

  6. iTunes 重新提交代码步骤

    1.选择View Details 2.右侧Links-Binary Details选项 3.Reject This Binary

  7. ThinkPHP视图查询详解

    ThinkPHP视图查询详解 参考http://www.jb51.net/article/51674.htm   这篇文章主要介绍了ThinkPHP视图查询,需要的朋友可以参考下     ThinkP ...

  8. spring mvc + mybatis + spring aop声明式事务管理没有作用

    在最近的一个项目中,采用springMVC.mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题! spring版本:3.0.5 myba ...

  9. 修改sqlserver2008中表的schema

    schema类似命名空间,相同schema中不能有同样的表名,不用schema下可以有相同的表名 修改schema的方法: 在数据库的 安全性->架构 中添加一个新的架构 找到要修改的表,右击设 ...

  10. css 梯形标签页

    html 代码 略 css : nav > a{ position: relative; display: inline_block; padding: .3em 1em 0; } nav &g ...