<>题目描述

<>初次尝试

public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { double l1Num =
getNum(l1); double l2Num = getNum(l2); double sum = l1Num + l2Num; ListNode temp
= new ListNode((int) (sum%10)); ListNode re = temp; sum = Math.floor(sum/10);
while (sum > 0) { int each = (int) (sum % 10); System.out.println(each);
ListNode listNode= new ListNode(each); temp.next = listNode; temp = listNode;
sum= Math.floor(sum/10); } return re; } public static double getNum(ListNode l1)
{ double sum = 0; int count = 0; while (l1 != null) { int val = l1.val; sum +=
val*Math.pow(10,count); l1 = l1.next; count++; } System.out.println(sum); return
sum; }
<>更换思路

boolean carry = false; //进位标志，注意要重置 ArrayList<Integer> l1ArrayList = new
ArrayList<>(); while (l1 != null) { l1ArrayList.add(l1.val); l1 = l1.next; } int
size1= l1ArrayList.size(); ArrayList<Integer> l2ArrayList = new ArrayList<>();
while (l2 != null) { l2ArrayList.add(l2.val); l2 = l2.next; } int size2 =
l2ArrayList.size(); int firstSum = l1ArrayList.get(0)+l2ArrayList.get(0);
ListNode temp= new ListNode(firstSum%10); ListNode re = temp; if (firstSum >=10)
{ carry = true; if (size1 ==1 && size2 == 1) { temp.next = new ListNode(firstSum
/10); } } for (int i = 1,j = 1; i<size1 || j<size2; i++,j++) { int num1 ; if (i
>= size1) { num1 = 0; } else { num1= l1ArrayList.get(i); } int num2 ; if (j >=
size2) { num2 = 0; } else { num2 = l2ArrayList.get(j); } int sum = num1 + num2;
ListNode eachTemp= new ListNode(sum%10); if (carry) { if (sum%10 +1 < 10) {
eachTemp= new ListNode(sum%10+1); carry = false; } else { eachTemp = new
ListNode(0); carry = true; } } temp.next = eachTemp; temp = eachTemp; if (sum >=
10) { carry = true; } } if (carry) { temp.next = new ListNode(1); } return re;

<>优化

boolean carry = false; //进位标志，注意要重置 int firstNum = l1.val + l2.val; ListNode
temp= new ListNode(firstNum%10); ListNode re = temp; l1 = l1.next; l2 = l2.next;
if (firstNum >= 10) { carry = true; if (l1 == null && l2 == null) { temp.next =
new ListNode(firstNum/10); } } while (l1 != null || l2 != null) { int num1; if (
l1== null) { num1 = 0; } else { num1 = l1.val; } int num2; if (l2 == null) {
num2= 0; } else { num2 = l2.val; } int sum = num1 + num2; ListNode eachTemp =
new ListNode(sum%10); if (carry) { if (sum % 10 < 9) { eachTemp = new ListNode(
sum%10+1); carry = false; } else { eachTemp = new ListNode(0); carry = true; } }
temp.next = eachTemp; temp = eachTemp; if (sum >= 10) { carry = true; } if (l1
!= null) { l1 = l1.next; } if (l2 != null) { l2 = l2.next; } } if (carry) { temp
.next = new ListNode(1); } return re;

<>大佬思路

* 他用了一个哑节点，用来处理头结点和尾节点，而我是分别特殊处理的
*

ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr =
dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ?
p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry =
sum/ 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p
= p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode