Skip to content

Commit

Permalink
feat: update leetcode 0206
Browse files Browse the repository at this point in the history
  • Loading branch information
benben6515 committed May 12, 2024
1 parent 455c6f2 commit ebf2b9b
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions leetcode-easy/0206-reverse-linked-list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
// Definition for singly-linked list.
class ListNode {
conststruct(val, next) {
this.val = val === undefined ? 0 : val
this.next = next === undefined ? null : next
}
}

/**
* @param {ListNode} head
* @return {ListNode}
Expand All @@ -17,8 +18,24 @@ var reverseList = function(head) {
head.next.next = head
head.next = null
return last
};
}

// 2022/04/24 done.
// Runtime: 85 ms, faster than 50.05% of JavaScript online submissions for Reverse Linked List.
// Memory Usage: 44.4 MB, less than 33.91% of JavaScript online submissions for Reverse Linked List.

// === Other solutions ===
var reverseList = function(head) {
let current = head
let lastNode = null
while (current) {
const node = new ListNode(current.val)
node.next = lastNode
lastNode = node
current = current.next
}
return lastNode
}
// 2024/05/12 done.
// Runtime: 69 ms Beats 9.01% of users with JavaScript
// Memory: 52.16 MB Beats 21.34% of users with JavaScript

0 comments on commit ebf2b9b

Please sign in to comment.