Skip to content

Commit 4b1c111

Browse files
committed
solve: reverse linked list
1 parent 25d31e6 commit 4b1c111

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

reverse-linked-list/wogha95.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
/**
2+
* 2차
3+
* Tony님 풀이 참고해서 SC 개선
4+
*
25
* TC: O(N)
36
* SC: O(1)
7+
* 순회동안 노드 생성하지 않으므로 공간복잡도가 상수다.
8+
*
9+
* N: linked-list length
10+
*/
11+
12+
/**
13+
* Definition for singly-linked list.
14+
* function ListNode(val, next) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.next = (next===undefined ? null : next)
17+
* }
18+
*/
19+
/**
20+
* @param {ListNode} head
21+
* @return {ListNode}
22+
*/
23+
var reverseList = function (head) {
24+
let pointer = null;
25+
26+
while (head !== null) {
27+
let temp = head.next;
28+
head.next = pointer;
29+
pointer = head;
30+
head = temp;
31+
}
32+
33+
return pointer;
34+
};
35+
36+
/**
37+
* 1차
38+
* TC: O(N)
39+
* linked-list 길이 만큼 순회
40+
*
41+
* SC: O(N)
42+
* linked-list 길이만큼 생성
43+
*
44+
* N: linked-list length
445
*/
546

647
/**

0 commit comments

Comments
 (0)