File tree Expand file tree Collapse file tree 1 file changed +25
-8
lines changed Expand file tree Collapse file tree 1 file changed +25
-8
lines changed Original file line number Diff line number Diff line change 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
+
8
9
/**
9
10
* @param {ListNode } head
10
11
* @return {ListNode }
@@ -17,8 +18,24 @@ var reverseList = function(head) {
17
18
head . next . next = head
18
19
head . next = null
19
20
return last
20
- } ;
21
+ }
21
22
22
23
// 2022/04/24 done.
23
24
// Runtime: 85 ms, faster than 50.05% of JavaScript online submissions for Reverse Linked List.
24
25
// 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
You can’t perform that action at this time.
0 commit comments