Skip to content

Commit b6c9cdc

Browse files
committed
reverse linked list solution
1 parent e08cf3a commit b6c9cdc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

reverse-linked-list/byol-han.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
* https://leetcode.com/problems/reverse-linked-list/
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
var reverseList = function (head) {
14+
let prev = null;
15+
let current = head;
16+
17+
while (current) {
18+
const next = current.next; // 다음 노드 기억
19+
current.next = prev; // 현재 노드가 이전 노드를 가리키도록 변경
20+
prev = current; // prev를 현재 노드로 이동
21+
current = next; // current를 다음 노드로 이동
22+
}
23+
24+
return prev; // prev는 새로운 head
25+
};

0 commit comments

Comments
 (0)