Skip to content

Commit

Permalink
[LeetCode] Add January 12.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 12, 2021
1 parent 2b730b9 commit 01362b0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions solved/LeetCode/Challenges/2020/January/12.kt
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 01362b0

Please sign in to comment.