Skip to content

Commit ba73034

Browse files
committed
Medium: 덧셈 문제 linkedlist 풀이이
1 parent 20516ff commit ba73034

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/*
15+
연결리스트 풀때 temp, node로 두어서 주로 푼다고함
16+
why?> node는 결과물을보기 위한 시작부분 포인터,
17+
temp는 포인터를 변경시키며 값을 변경시키는 용도인듯함.
18+
*/
19+
const node = new ListNode()
20+
let temp = node
21+
let carry = 0;
22+
23+
while(l1||l2||carry){
24+
let SumData = carry;
25+
if(l1){
26+
SumData +=l1.val
27+
l1=l1.next
28+
}
29+
if(l2){
30+
SumData +=l2.val
31+
l2=l2.next
32+
}
33+
carry = Math.floor(SumData/10)
34+
temp.next = new ListNode(SumData%10)
35+
temp=temp.next
36+
}
37+
return node.next
38+
};

0 commit comments

Comments
 (0)