We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 084fbdb commit 42a4897Copy full SHA for 42a4897
2-add-two-numbers/2-add-two-numbers.js
@@ -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