题目描述
假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。
给定两个这种链表,请生成代表两个整数相加值的结果链表。
例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。
输入描述:
第一行两个整数 n 和 m,分别表示两个链表的长度。
第二行 n 个整数 ai 表示第一个链表的节点。
第三行 m 个整数 bi 表示第二个链表的节点。
输出描述:
输出一行整数表示结果链表。
示例1
输入
3 2
9 3 7
6 3
输出
1 0 0 0
解法一:自己写的
思路: 创建树的时候用了倒序插入(应该先创建一个正常顺序树然后自己倒序再按此方法处理)
import java.io.*;
public class Main{
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//头插
public static ListNode creat(int len,String[] ss){
ListNode head = new ListNode(0);
for(int i=0;i<len;i++){
ListNode node = new ListNode(Integer.parseInt(ss[i]));
node.next = head.next;
head.next = node;
}
return head.next;
}
public static ListNode add(ListNode h1,ListNode h2){
ListNode head = new ListNode(0);
int r = 0;
while(h1!=null&&h2!=null){
int sum = h1.val+h2.val+r;
ListNode node =new ListNode(sum%10);
node.next = head.next;
head.next = node;
r = sum/10;
h1 = h1.next;
h2 = h2.next;
}
while(h1!=null){
int sum = h1.val+r;
ListNode node =new ListNode(sum%10);
node.next = head.next;
head.next = node;
r = sum/10;
h1 = h1.next;
}
while(h2!=null){
int sum = h2.val+r;
ListNode node =new ListNode(sum%10);
node.next = head.next;
head.next = node;
r = sum/10;
h2 = h2.next;
}
if(r!=0){
ListNode node =new ListNode(r);
node.next = head.next;
head.next = node;
}
return head.next;
}
public static void main(String[] args)throws IOException{
String[] lens = br.readLine().trim().split(" ");
int len1 = Integer.parseInt(lens[0]);
int len2 = Integer.parseInt(lens[1]);
String[] ss1 = br.readLine().trim().split(" ");
String[] ss2 = br.readLine().trim().split(" ");
ListNode h1 = creat(len1,ss1);
ListNode h2 = creat(len2,ss2);
ListNode head = add(h1,h2);
StringBuilder sb = new StringBuilder();
while(head!=null){
sb.append(head.val).append(" ");
head = head.next;
}
System.out.print(sb.toString());
}
}
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val = val;
this.next = null;
}
}
左程云的方法:前提是两个正常顺序树
方法一: 利用两个栈
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] lens = br.readLine().trim().split(" ");
int len1 = Integer.parseInt(lens[0]);
int len2 = Integer.parseInt(lens[1]);
String[] s1 = br.readLine().trim().split(" ");
String[] s2 = br.readLine().trim().split(" ");
ListNode h1 = new ListNode(-1);
ListNode h2 = new ListNode(-1);
for(int i=len1-1;i>=0;i--){
ListNode node = new ListNode(Integer.parseInt(s1[i]));
node.next = h1.next;
h1.next = node;
}
for(int i=len2-1;i>=0;i--){
ListNode node = new ListNode(Integer.parseInt(s2[i]));
node.next = h2.next;
h2.next = node;
}
ListNode res = sum(h1.next,h2.next);
StringBuilder sb = new StringBuilder();
while(res!=null){
sb.append(res.val+" ");
res = res.next;
}
System.out.println(sb.toString().trim());
}
public static ListNode sum(ListNode h1,ListNode h2){
if(h1==null&&h2==null) return null;
if(h1==null) return h2;
if(h2==null) return h1;
//用栈
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
while(h1!=null){
s1.push(h1.val);
h1 = h1.next;
}
while(h2!=null){
s2.push(h2.val);
h2 = h2.next;
}
ListNode head = new ListNode(-1);
int r = 0;//进位
while(!s1.isEmpty()&&!s2.isEmpty()){
int tmp = s1.pop()+s2.pop()+r;
r = tmp/10;
tmp = tmp%10;
ListNode node = new ListNode(tmp);
node.next = head.next;
head.next = node;
}
while(!s1.isEmpty()){
int tmp = s1.pop()+r;
r = tmp/10;
tmp = tmp%10;
ListNode node = new ListNode(tmp);
node.next = head.next;
head.next = node;
}
while(!s2.isEmpty()){
int tmp = s2.pop()+r;
r = tmp/10;
tmp = tmp%10;
ListNode node = new ListNode(tmp);
node.next = head.next;
head.next = node;
}
if(r!=0){
ListNode node = new ListNode(r);
node.next = head.next;
head.next = node;
}
return head.next;
}
}
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val = val;
this.next = null;
}
}
方法二: 先把树倒序,然后再按上述方法,之后再变回来