「リートコード - 2. Add Two Numbers」 发表于 2021-07-20 | 分类于 ジャバ ジャバソリューション1234567891011121314151617181920212223242526272829303132/** * 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) { ListNode res = new ListNode((l1.val + l2.val) % 10), tail = res; int carry = (l1.val + l2.val) / 10; ListNode u = l1.next, v = l2.next; while (carry != 0 || u != null || v != null) { int sum = carry; if (u != null) { sum += u.val; u = u.next; } if (v != null) { sum += v.val; v = v.next; } tail.next = new ListNode(sum % 10); tail = tail.next; carry = sum / 10; } return res; }} 明天one-one聊promotion,紧张ing。 本文作者: XIAO TUO 本文链接: http://tashi711.top/java/leetcode-2/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!