import java.util.Scanner;
import java.util.function.IntToDoubleFunction;
import javax.lang.model.util.ElementScanner6;
public class twoNumAdd {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入第一个数字:");
long value1 = in.nextLong();
System.out.println("输入第二个数字");
long value2 = in.nextLong();
ListNode l1 = solusion.longToList(value1);
ListNode l2 = solusion.longToList(value2);
ListNode l3 = solusion.addTwoNumbers(l1, l2);
StringBuffer sb = solusion.listToSb(l3);
System.out.println(sb);
}
}
class solusion {
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//提交部分
int temp = l1.val + l2.val;//最低位相加
boolean add;//进位标识
ListNode l3;//加和链表
if (temp >= 10) {//若低位相加超过10,则将temp低位赋给l3,同时进位标识置为true
l3 = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
add=true;
}
else {//若低位相加未超过10,则temp唯一一位赋给l3,进位标志置为false
l3 = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
add = false;
}
ListNode L = l3;//存储链表头
l1=l1.next;l2=l2.next;
if (l1==null&&l2==null&&add){//若此时l1,l2已结束且add为真,进位
l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
l3 = l3.next;
add = false;
}
while (l1!= null || l2!= null||add) {
if (l1 != null && l2 != null) {//l1,l2均为加完
if (add) {//若有进位,需额外+1
temp = l1.val + l2.val + 1;
} else {
temp = l1.val + l2.val;
}
if (temp >= 10) {//同理
l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
add = true;
} else {
l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(0))));
add = false;
}
l1 = l1.next;
l2 = l2.next;
}
else if (l1 == null && l2 != null) {//l1已加完
if (add) {//若有进位,额外+1
temp=l2.val+1;
if (temp >= 10) {//若+1后有进位,同理
l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
if (l2.next == null) {
l2.next = new ListNode(0);
}
l2 = l2.next;
add = true;
} else {
l3.next = new ListNode(l2.val + 1);
l2 = l2.next;
add = false;
}
}
else {
l3.next = new ListNode(l2.val);
l2 = l2.next;
}
}
else if (l1 != null && l2 == null) {//l2已加完
if (add) {//同理
temp = l1.val + 1;
if (temp >= 10) {
l3.next = new ListNode(Integer.parseInt(String.valueOf(String.valueOf(temp).charAt(1))));
if (l1.next == null) {
l1.next = new ListNode(0);
}
l1 = l1.next;
add = true;
} else {
l3.next = new ListNode(l1.val + 1);
l1 = l1.next;
add = false;
}
} else {
l3.next = new ListNode(l1.val);
l1 = l1.next;
}
} else if (l1 == null && l2 == null && add) {//l1,l2均已加完,但是还有进位
l3.next = new ListNode(1);
add = false;
}
l3 = l3.next;
}
return L;
}
public static ListNode longToList(long value) {//整型转化为链表
String str = String.valueOf(value);
int len = str.length();
ListNode L = new ListNode(Integer.parseInt(String.valueOf(str.charAt(len - 1))));
ListNode LHead = L;
len--;
while (len > 0) {
L.next = new ListNode(Integer.parseInt(String.valueOf(str.charAt(len - 1))));
len--;
L = L.next;
}
return LHead;
}
public static StringBuffer listToSb(ListNode l) {//链表转化为字符串
StringBuffer sb = new StringBuffer();
while (l.next != null) {
sb.append(String.valueOf(l.val));
l = l.next;
}
sb.append(String.valueOf(l.val));
return sb;
}
}