
题解
辅助栈

class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Stack<ListNode> stack1 = new Stack<>(); Stack<ListNode> stack2 = new Stack<>(); while (l1 != null) { stack1.push(l1); l1 = l1.next; } while (l2 != null) { stack2.push(l2); l2 = l2.next; } // 记录是否进位 int tip = 0; ListNode ans = null; while (!stack1.isEmpty() || !stack2.isEmpty() || tip != 0) { int l1Val = stack1.isEmpty() ? 0 : stack1.pop().val; int l2Val = stack2.isEmpty() ? 0 : stack2.pop().val; int sum = l1Val + l2Val + tip; tip = sum / 10; ListNode curNode = new ListNode(sum % 10); curNode.next = ans; ans = curNode; } return ans; }}