object Main extends App {
def idleTime(l1: ListNode, l2: ListNode) = {
val dummyHead = new ListNode(0)
var p = l1
var q = l2
var curr = dummyHead
var carry = 0
while ( {
p != null || q != null
}) {
val x = if (p != null) p.`val`
else 0
val y = if (q != null) q.`val`
else 0
val sum = carry + x + y
carry = sum / 10
curr.next = new ListNode(sum % 10)
curr = curr.next
if (p != null) p = p.next
if (q != null) q = q.next
}
if (carry > 0)
curr.next = new ListNode(carry)
dummyHead.next
}
var list1 = new ListNode(9)
list1.next = new ListNode(9)
list1.next.next = new ListNode(9)
val list2 = new ListNode(9)
list2.next = new ListNode(9)
list2.next.next = new ListNode(9)
val dummy = idleTime(list1, list2)
dummy.printList
}