g0001_0100.s0002_add_two_numbers.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0002_add_two_numbers
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
// #2022_09_22_Time_417_ms_(50.44%)_Space_50.5_MB_(35.36%)
import com_github_leetcode.ListNode
/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
val dummyHead = ListNode(0)
var p = l1
var q = l2
var curr: ListNode? = dummyHead
var carry = 0
while (p != null || q != null) {
val x = p?.`val` ?: 0
val y = q?.`val` ?: 0
val sum = carry + x + y
carry = sum / 10
curr!!.next = ListNode(sum % 10)
curr = curr.next
if (p != null) {
p = p.next
}
if (q != null) {
q = q.next
}
}
if (carry > 0) {
curr!!.next = ListNode(carry)
}
return dummyHead.next
}
}