Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions algorithm/javascript/leetcode/2-add-two-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://leetcode.com/problems/add-two-numbers/

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
let node = null;
const carry = arguments[2];
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반올림 하는경우

if (l1 || l2) {
const val1 = l1 ? l1.val : 0;
const val2 = l2 ? l2.val : 0;
const next1 = l1 ? l1.next : null;
const next2 = l2 ? l2.next : null;
const val = carry ? val1 + val2 + 1 : val1 + val2;
node = new ListNode(val % 10);
node.next = addTwoNumbers(next1, next2, val >= 10);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 이 경우 node list 가 되는거지??

} else if (carry) {
node = new ListNode(1);
node.next = null;
}
return node;
};