cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum

时间:2021-03-08 06:43:04

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;
}
}