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 114826c commit abc0ce4Copy full SHA for abc0ce4
Add Two Numbers/code.js
@@ -0,0 +1,28 @@
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
+ const beforeHead = new ListNode();
15
+ let curr = beforeHead;
16
+ let carry = false;
17
+ while (l1 || l2 || carry) {
18
+ const a = (l1 || 0) && l1.val;
19
+ const b = (l2 || 0) && l2.val;
20
+ const sum = a + b + carry;
21
+ curr.next = new ListNode(sum % 10);
22
+ curr = curr.next;
23
+ l1 = l1 && l1.next;
24
+ l2 = l2 && l2.next;
25
+ carry = sum >= 10;
26
+ }
27
+ return beforeHead.next;
28
+};
0 commit comments