My Accepted Scala Solution


  • 0
    K

    def addTwoNumbers(l1: ListNode, l2: ListNode): ListNode = {
    var carry = 0
    var L1 = l1
    var L2 = l2
    var pre = new ListNode(0)
    val head = pre
    while (L1 != null || L2 !=null || carry !=0){
    var value1,value2 = 0
    if(L1 != null){
    value1 = L1._x
    L1 = L1.next
    }
    if(L2 != null){
    value2 = L2._x
    L2 = L2.next
    }
    val cur = new ListNode((value1+value2+carry) % 10)
    carry = (value1+value2+carry) / 10
    pre.next = cur
    pre = cur
    }
    head.next
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.