import java.util.*;
public class Main{
public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
long a=0;
int ac=0;
long b=0;
int bc=0;
long c=0;
while(l1!=null){
a+=l1.val*Math.pow(10,ac);
ac++;
l1=l1.next;
}
while(l2!=null){
b+=l2.val*Math.pow(10,bc);
bc++;
l2=l2.next;
}
c=a+b;
//System.out.println(c);
ListNode l[]=new ListNode[100];
for(int i=0;i<l.length;i++) {
l[i]=new ListNode();
}
int i=0;
while(c!=0){
l[i].val=(int)(c%10);
if(i>=1)
l[i-1].next=l[i];
c=c/10;
i++;
}
return l[0];
}
public static void main(String[] args) {
//Scanner in = new Scanner(System.in);
//int a = in.nextInt();
//System.out.println(a);
ListNode firstNode1=new ListNode(9);
ListNode secondNode1=new ListNode(9);
ListNode firstNode2= new ListNode(9);
ListNode secondNode2=new ListNode(9);
ListNode firstNode3=new ListNode(9);
ListNode secondNode3=new ListNode(9);
ListNode firstNode4= new ListNode(9);
ListNode secondNode4=new ListNode(9);
ListNode secondNode5=new ListNode(9);
ListNode l1=new ListNode(9);
ListNode l2=new ListNode(1);
ListNode l3=new ListNode();
l1.next=firstNode1;
firstNode1.next=secondNode1;
secondNode1.next=firstNode2;
firstNode2.next=secondNode2;
secondNode2.next=secondNode3;
secondNode3.next=firstNode3;
firstNode3.next=secondNode4;
secondNode4.next=firstNode4;
firstNode4.next=secondNode5;
l3=addTwoNumbers(l1,l2);
while(l3!=null){
System.out.println(l3.val);
l3=l3.next;
}
//System.out.println("Hello World!");
}
}
class ListNode{
int val;
ListNode next;
ListNode() {}
ListNode(int val){this.val=val;}
ListNode(int val,ListNode next){this.val=val;this.next=next;}
}
a hahahhaha,不行哦,,太浪费空间了,而且太大了,,还是得用进位来,,呵呵呵呵
class Solution {
public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
int carry=0;
ListNode l[]=new ListNode[100];
for(int i=0;i<l.length;i++) {
l[i]=new ListNode(0);
}
int i=0;
while(l1!=null||l2!=null){
int n1= l1!=null?l1.val:0;
int n2= l2!=null?l2.val:0;
int sum=n1+n2+carry;
l[i].val=sum%10;
if(i>=1)
l[i-1].next=l[i];
carry=sum/10;
if(l1!=null)
l1=l1.next;
if(l2!=null)
l2=l2.next;
i++;
}
if(carry>0){
l[i].val=carry;
if(i>=1)
l[i-1].next=l[i];
}
return l[0];
}
}