Skip to content

Commit e0edb9d

Browse files
authored
Merge pull request #932 from yeeZinu/main
[hodoli] Week 7
2 parents 8deb55d + 238528d commit e0edb9d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

reverse-linked-list/yeeZinu.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* 문제: 리스트 뒤집기
10+
*
11+
* 시간 복잡도: O(n)
12+
* 공간 복잡도: O(n)
13+
*/
14+
/**
15+
* @param {ListNode} head
16+
* @return {ListNode}
17+
*/
18+
var reverseList = function(head) {
19+
// 노드 값을 선언
20+
let node = null;
21+
22+
// head가 없을 때 까지 반복
23+
while(head) {
24+
const temp = head.next; // head 의 다음 값을 temp에 저장
25+
head.next = node; // 기존 head의 다음값을 node에 저장
26+
node = head; // 현재 node의 값을 head에 저장
27+
head = temp; // 현재 head값에 temp 저장
28+
}
29+
30+
return node; // node 출력
31+
};

0 commit comments

Comments
 (0)