445. Add Two Numbers II 发表于 2021-12-04 1234567891011121314151617181920212223242526272829303132333435class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(); Stack<Integer> stackA = new Stack<>(); Stack<Integer> stackB = new Stack<>(); while (l1 != null) { stackA.push(l1.val); l1 = l1.next; } while (l2 != null) { stackB.push(l2.val); l2 = l2.next; } int carry = 0; while (!stackA.isEmpty() || !stackB.isEmpty() || carry != 0) { int sum = carry; if (!stackA.isEmpty()) { sum += stackA.pop(); } if (!stackB.isEmpty()) { sum += stackB.pop(); } ListNode next = dummyHead.next; dummyHead.next = new ListNode(sum % 10); dummyHead.next.next = next; carry = sum / 10; } return dummyHead.next; }} Reference445. Add Two Numbers II剑指 Offer II 025. 链表中的两数相加