Skip to content

Commit

Permalink
Merge pull request neetcode-gh#553 from adamMRamos/scala/2-add-two-nu…
Browse files Browse the repository at this point in the history
…mbers

Scala/2-add-two-numbers
  • Loading branch information
Ahmad-A0 authored Jul 21, 2022
2 parents 433d47a + 8448007 commit 168273d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scala/2-Add-Two-Numbers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
object Solution {
def addTwoNumbers(l1: ListNode, l2: ListNode): ListNode = {
def nonNull(l: ListNode): Boolean = l != null
def next(l: ListNode): ListNode = if (nonNull(l)) l.next else null
def getX(l: ListNode): Int = if (nonNull(l)) l.x else 0
def sumX(l1: ListNode, l2: ListNode, carry: Int): (ListNode, Int) = {
val sum = getX(l1) + getX(l2) + carry
new ListNode(sum % 10) -> sum / 10
}

var carry = 0
val (sum, newCarry) = sumX(l1, l2, carry)
carry = newCarry

var nextDigit = sum
var next1 = next(l1)
var next2 = next(l2)

while (nonNull(next1) || nonNull(next2)) {
val (sum, newCarry) = sumX(next1, next2, carry)

nextDigit.next = sum
nextDigit = nextDigit.next

carry = newCarry
next1 = next(next1)
next2 = next(next2)
}

if (carry > 0)
nextDigit.next = new ListNode(carry)

sum
}
}

0 comments on commit 168273d

Please sign in to comment.