Algorithm:
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode node = new ListNode(null);
ListNode p = node;
int carry = 0;
while (l1 != null || l2 != null) {
int sum = carry;
if (l1 != null && l1.val != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null && l2.val != null) {
sum += l2.val;
l2 = l2.next;
}
carry = sum / 10;
p.next = new ListNode(sum % 10);
p = p.next;
}
if (carry > 0) {
p.next = new ListNode(1);
}
return node.next;
}
Tip:
Redis过期机制 https://www.jianshu.com/p/0e13fcdc8fa1
Share:
Java内存模型 https://time.geekbang.org/column/article/13484