-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
/** | ||
*Definition for singly-linked list. | ||
*/ | ||
var ListNode = function(val) { | ||
this.val = val; | ||
this.next = null; | ||
} | ||
|
||
/** | ||
* @param {ListNode} l1 | ||
* @param {ListNode} l2 | ||
* @return {ListNode} | ||
*/ | ||
var addTwoNumbers = function(l1, l2) { | ||
|
||
if (!l1 || !l2) { return null;} | ||
|
||
var a = l1.val + l2.val; | ||
var bu = parseInt(a / 10); | ||
a = a % 10; | ||
var resultNode = new ListNode(a); | ||
|
||
var current = resultNode; | ||
|
||
|
||
var node1 = l1.next; | ||
var node2 = l2.next; | ||
|
||
while(node1 || node2) { | ||
a = (node1 && node1.val) + (node2 && node2.val) + bu; | ||
// 获取数量之和 + 上一次的进位 | ||
bu = parseInt(a /10) | ||
|
||
a = a % 10; | ||
|
||
var nextNode = new ListNode(a) | ||
|
||
current.next = nextNode; | ||
current = nextNode; | ||
|
||
node1 = node1 && node1.next; | ||
node2 = node2 && node2.next; | ||
} | ||
|
||
if (bu) {current.next = new ListNode(bu);} | ||
|
||
return resultNode; | ||
}; | ||
|
||
function buildNodeList(arr) { | ||
if (!arr || !arr.length) { return null;} | ||
var result = new ListNode(arr[0]); | ||
var i = 1, len = arr.length; | ||
var next = null; | ||
var current = result; | ||
for(; i < len; i++) { | ||
next = new ListNode(arr[i]); | ||
current.next = next; | ||
current = next; | ||
} | ||
return result; | ||
} | ||
|
||
console.log(buildNodeList([3,4,2,5])); | ||
console.log(buildNodeList([0])); | ||
console.log(addTwoNumbers(buildNodeList([1,8]), buildNodeList([0]))); |