Skip to content

Commit d54704a

Browse files
committed
add js soultion for leetcode 141 142 206
1 parent 34d0395 commit d54704a

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| |Medium|
227227
|208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)| |Medium|
228228
|207|[Course Schedule](https://leetcode.com/problems/course-schedule/)| |Medium|
229-
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [java](./algorithms/reverseLinkedList/Solution.java) |Easy|
229+
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [java](./algorithms/reverseLinkedList/Solution.java), [js](./algorithms/reverseLinkedList/reverseLinkedList.js) |Easy|
230230
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)| |Easy|
231231
|204|[Count Primes](https://leetcode.com/problems/count-primes/)| |Easy|
232232
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)| [js](./algorithms/removeLinkedListElements/removeLinkedListElements.js),[java](./algorithms/removeLinkedListElements/Solution.java) |Easy|
@@ -284,8 +284,8 @@
284284
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [js](./algorithms/binaryTreePostorderTraversal/binaryTreePostorderTraversal.js) |Hard|
285285
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)| [js](./algorithms/binaryTreePreorderTraversal/binaryTreePreorderTraversal.js) |Medium|
286286
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)| |Medium|
287-
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| |Medium|
288-
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| |Medium|
287+
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| [js](./algorithms/linkedListCycleII/linkedListCycleII.js) |Medium|
288+
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [js](./algorithms/linkedListCycle/linkedListCycle.js) |Medium|
289289
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)| |Hard|
290290
|139|[Word Break](https://leetcode.com/problems/word-break/)| |Medium|
291291
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| |Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var hasCycle = function(head) {
2+
if (!head || !head.next) return false;
3+
let slow = head, fast = head.next;
4+
while (fast) {
5+
if (slow.val === fast.val) return true;
6+
if (!fast.next || !fast.next.next) return false;
7+
fast = fast.next.next;
8+
slow = slow.next;
9+
}
10+
return false;
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var detectCycle = function(head) {
2+
if (head === null) return null;
3+
let slow = head, fast = head;
4+
while (fast !== null) {
5+
// 慢指针每次走 1步
6+
slow = slow.next;
7+
if (fast.next === null) return null;
8+
// 快指针每次走2步
9+
fast = fast.next.next;
10+
// 快慢指针相遇
11+
if (fast === slow) {
12+
// 这时把快指针重新指向头部节点
13+
let ptr = head;
14+
// 当快慢指针相遇的时候,就是入环节点的位置
15+
while (ptr !== slow) {
16+
ptr = ptr.next;
17+
slow = slow.next;
18+
}
19+
return ptr;
20+
}
21+
22+
}
23+
return null;
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var reverseList = function(head) {
2+
let prev = null;
3+
let curr = head;
4+
while(curr) {
5+
let nextTemp = curr.next;
6+
curr.next = prev;
7+
prev = curr;
8+
curr = nextTemp;
9+
}
10+
return prev;
11+
};

0 commit comments

Comments
 (0)