From 01362b0138a5420bbe2d528f619a10a7a2ac22ea Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Tue, 12 Jan 2021 16:03:33 +0100 Subject: [PATCH] [LeetCode] Add January 12. --- solved/LeetCode/Challenges/2020/January/12.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/12.kt diff --git a/solved/LeetCode/Challenges/2020/January/12.kt b/solved/LeetCode/Challenges/2020/January/12.kt new file mode 100644 index 00000000..bed27a55 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/12.kt @@ -0,0 +1,25 @@ +class Solution { + fun addTwoNumbers(A: ListNode?, B: ListNode?): ListNode? { + var l1 = A + var l2 = B + var head = ListNode(0) + var current = head + var carry = 0 + + fun canAdd() = l1 != null || l2 != null || carry != 0 + + while (canAdd()) { + val here = (l1?.`val` ?: 0) + (l2?.`val` ?: 0) + carry + current.`val` = here % 10 + carry = here / 10 + l1 = l1?.let { it.next } + l2 = l2?.let { it.next } + if (canAdd()) { + current.next = ListNode(0) + current = current.next + } + } + + return head + } +}