-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.kts
More file actions
30 lines (24 loc) · 754 Bytes
/
2.kts
File metadata and controls
30 lines (24 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class ListNode(var `val`: Int) {
var next: ListNode? = null
}
class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode {
val initialNode = ListNode(0)
var curNode: ListNode? = null
var nextNode = initialNode
var node1 = l1
var node2 = l2
var remain = 0
while (node1 != null || node2 != null || remain != 0) {
curNode?.next = nextNode
curNode = nextNode
nextNode = ListNode(0)
val value = (node1?.`val` ?: 0) + (node2?.`val` ?: 0) + remain
remain = value / 10
curNode.`val` = value % 10
node1 = node1?.next
node2 = node2?.next
}
return initialNode
}
}