Skip to content

Commit 5d21aff

Browse files
committed
✨ [206] Reverse Linked List
Runtime 60 ms Beats 77.47% Memory 43.7 MB Beats 95.22%
1 parent 185eb9e commit 5d21aff

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

206/my_solution.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
class ListNode {
9+
constructor(val, next) {
10+
this.val = (val === undefined ? 0 : val);
11+
this.next = (next === undefined ? null : next);
12+
}
13+
}
14+
/**
15+
* @param {ListNode} head
16+
* @return {ListNode}
17+
*/
18+
const reverseList = (head) => {
19+
let dummy = new ListNode(), new_head = dummy;
20+
21+
while (head !== null) {
22+
dummy.next = new ListNode(head.val, dummy.next);
23+
head = head.next;
24+
}
25+
26+
return new_head.next;
27+
};

206/solution.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const reverseList = (head) => {
13+
let dummy = new ListNode(), new_head = dummy;
14+
15+
while (head !== null) {
16+
dummy.next = new ListNode(head.val, dummy.next);
17+
head = head.next;
18+
19+
console.log("dummy")
20+
console.log(dummy)
21+
console.log("dummy.next")
22+
console.log(dummy.next)
23+
// dummy = dummy.next;
24+
}
25+
26+
return new_head.next;
27+
};
28+
29+
reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))))

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [191. Number of 1 Bits](./191/)
2929
- [198. House Robber](./198/)
3030
- [200. Number of Islands](./200/)
31+
- [206. Reverse Linked List](./206/)
3132
- [213. House Robber II](./213/)
3233
- [217. Contains Duplicate](./217/)
3334
- [238. Product of Array Except Self](./238/)
@@ -61,10 +62,11 @@ iex ./.../solution.ex
6162

6263
---
6364

64-
Quick create in bash
65+
Batch create in bash
6566
```ssh
66-
chapter=21 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
67+
chapter=206 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
6768
```
69+
> And now you can use `x` for quick debug.
6870
6971
<!--
7072
TODO: Add to TOC!

0 commit comments

Comments
 (0)