Skip to content

Commit 3c172da

Browse files
committed
solve: reverse linked list
1 parent 67ab0cd commit 3c172da

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

โ€Žreverse-linked-list/wogha95.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(1)
4+
*/
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* function ListNode(val, next) {
9+
* this.val = (val===undefined ? 0 : val)
10+
* this.next = (next===undefined ? null : next)
11+
* }
12+
*/
13+
/**
14+
* @param {ListNode} head
15+
* @return {ListNode}
16+
*/
17+
var reverseList = function (head) {
18+
let pointer = null;
19+
20+
while (head) {
21+
// 1. ์ •๋‹ต ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์•ž์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€
22+
pointer = new ListNode(head.val, pointer);
23+
// 2. head๋Š” ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
24+
head = head.next;
25+
}
26+
27+
return pointer;
28+
};

0 commit comments

Comments
ย (0)