Skip to content

Commit 9536df1

Browse files
authored
Create 206-Reverse-Linked-List.js
1 parent a8e62c9 commit 9536df1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

javascript/206-Reverse-Linked-List.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function(head) {
13+
let prev = null;
14+
15+
while (head) {
16+
let next = head.next;
17+
head.next = prev;
18+
prev = head;
19+
head = next;
20+
}
21+
22+
return prev;
23+
};

0 commit comments

Comments
 (0)