Skip to content

Commit ebf2b9b

Browse files
committed
feat: update leetcode 0206
1 parent 455c6f2 commit ebf2b9b

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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-
*/
1+
// Definition for singly-linked list.
2+
class ListNode {
3+
conststruct(val, next) {
4+
this.val = val === undefined ? 0 : val
5+
this.next = next === undefined ? null : next
6+
}
7+
}
8+
89
/**
910
* @param {ListNode} head
1011
* @return {ListNode}
@@ -17,8 +18,24 @@ var reverseList = function(head) {
1718
head.next.next = head
1819
head.next = null
1920
return last
20-
};
21+
}
2122

2223
// 2022/04/24 done.
2324
// Runtime: 85 ms, faster than 50.05% of JavaScript online submissions for Reverse Linked List.
2425
// Memory Usage: 44.4 MB, less than 33.91% of JavaScript online submissions for Reverse Linked List.
26+
27+
// === Other solutions ===
28+
var reverseList = function(head) {
29+
let current = head
30+
let lastNode = null
31+
while (current) {
32+
const node = new ListNode(current.val)
33+
node.next = lastNode
34+
lastNode = node
35+
current = current.next
36+
}
37+
return lastNode
38+
}
39+
// 2024/05/12 done.
40+
// Runtime: 69 ms Beats 9.01% of users with JavaScript
41+
// Memory: 52.16 MB Beats 21.34% of users with JavaScript

0 commit comments

Comments
 (0)