Skip to content

Commit 42a4897

Browse files
committed
Time: 146 ms (58.38%), Space: 47 MB (21.45%) - LeetHub
1 parent 084fbdb commit 42a4897

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function(l1, l2) {
14+
let head = new ListNode();
15+
curr = head;
16+
remainder = 0;
17+
18+
while(l1 != null || l2 != null){
19+
let x = l1 !== null?l1.val:0
20+
let y = l2 !== null?l2.val:0
21+
sum = x+y+remainder
22+
remainder = Math.floor(sum/10)
23+
24+
curr.next = new ListNode(sum%10)
25+
curr = curr.next
26+
27+
if(l1 !== null) l1 = l1.next
28+
if(l2 !== null) l2 = l2.next
29+
}
30+
31+
if(remainder>0) curr.next = new ListNode(1)
32+
33+
return head.next
34+
};

0 commit comments

Comments
 (0)