🟡 剑指 Offer II 025. 链表中的两数相加
LeetCode 提示
题目难度 中等
原题链接 🔗 leetcode
#
题解1主要是工程问题。用栈来存所有数字,只需遍历两次即可得到结果。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Deque<Integer> s1 = new LinkedList<>(); Deque<Integer> s2 = new LinkedList<>();
while (l1 != null) { s1.add(l1.val); l1 = l1.next; }
while (l2 != null) { s2.add(l2.val); l2 = l2.next; }
int up = 0; ListNode res = null; while (!s1.isEmpty() || !s2.isEmpty()) { int cnt = up; if (!s1.isEmpty()) { cnt += s1.removeLast(); } if (!s2.isEmpty()) { cnt += s2.removeLast(); }
ListNode cur = new ListNode(cnt % 10, res); res = cur; up = cnt / 10; }
if (up > 0) { ListNode cur = new ListNode(up, res); res = cur; }
return res; }}