Skip to content

Commit abc0ce4

Browse files
committed
added Description
1 parent 114826c commit abc0ce4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Add Two Numbers/code.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)