Skip to content

Commit e2b5f45

Browse files
committed
chore: 更新部分写法
1 parent 458c7ab commit e2b5f45

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ TypeScript / JavaScript 基础算法、数据结构练习,包含 LeetCode 或
203203
输入:nums = [6, 7, 0, 3, 4], target = 5
204204
输出:false
205205

206-
- [循环数组中的环-暂无解](src/array/circular-array-loop.ts)
206+
- [循环数组中的环](src/array/circular-array-loop.ts)
207207

208208
- LeetCode 457. 环形数组循环 <https://leetcode-cn.com/problems/circular-array-loop/>
209209
- LintCode 1229. 循环数组中的环 <https://www.lintcode.com/problem/circular-array-loop/description>

src/list/delete-node-in-a-linked-list.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import ListNode from "../lib/ListNode"
77
/**
88
Do not return anything, modify it in-place instead.
99
*/
10-
export function deleteNode (root: ListNode | null): void {
11-
if (root === null) {
10+
export function deleteNode (node: ListNode | null): void {
11+
if (node === null) {
1212
return
1313
}
14-
if (root.next === null) {
15-
root = null
14+
if (node.next === null) {
15+
node = null
1616
return
1717
}
18-
root.val = root.next.val
19-
root.next = root.next.next
18+
node.val = node.next.val
19+
node.next = node.next.next
2020
}

src/list/linked-list-cycle.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import ListNode from '../lib/ListNode'
22

3-
/**
4-
* @param {ListNode} head
5-
* @return {boolean}
6-
*/
7-
export const hasCycle = function (head: ListNode | null): boolean {
8-
if (!head || !head.next) return false
9-
10-
// 双指针解法
11-
let slow: ListNode = head
12-
let fast: ListNode = head.next
3+
// export const hasCycle = function (head: ListNode | null): boolean {
4+
// const set = new Set()
5+
// let current = head
6+
// while (current && current.next !== null) {
7+
// if (set.has(current)) {
8+
// return true
9+
// }
10+
// set.add(current)
11+
// current = current.next
12+
// }
13+
// return false
14+
// }
1315

14-
while (fast && fast.next && fast.next.next && slow!.next) {
15-
if (fast.next === slow || fast === slow) return true
16-
fast = fast!.next!.next
17-
slow = slow!.next
16+
export const hasCycle = function (head: ListNode | null): boolean {
17+
let slow = head
18+
let fast = head?.next
19+
while (slow?.next && fast?.next?.next) {
20+
if (fast.next === slow || slow === fast) return true
21+
slow = slow.next
22+
fast = fast.next.next
1823
}
19-
2024
return false
2125
}
26+
27+

0 commit comments

Comments
 (0)