-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbers.js
More file actions
36 lines (35 loc) · 786 Bytes
/
addTwoNumbers.js
File metadata and controls
36 lines (35 loc) · 786 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
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const addTwoNumbers = (l1, l2) => {
const stack = []
let remainder = 0
let list = new ListNode()
const mainList = list
while (l1 || l2 || remainder) {
const l1Node = l1?.val || 0
const l2Node = l2?.val || 0
let sum = l1Node + l2Node + remainder
if (sum > 9) {
remainder = 1
sum = sum % 10
} else {
remainder = 0
}
const currentNode = new ListNode(sum)
list.next = currentNode
list = currentNode
l1 = l1?.next
l2 = l2?.next
}
return mainList.next
}