Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5268ee

Browse files
添加(0141.环形链表.md):增加typescript版本
1 parent 9c32528 commit f5268ee

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

‎problems/0141.环形链表.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
# 141. 环形链表
99

10+
[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/)
11+
1012
给定一个链表,判断链表中是否有环。
1113

1214
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
@@ -103,7 +105,7 @@ class Solution:
103105
return False
104106
```
105107

106-
## Go
108+
### Go
107109

108110
```go
109111
func hasCycle(head *ListNode) bool {
@@ -139,6 +141,23 @@ var hasCycle = function(head) {
139141
};
140142
```
141143

144+
### TypeScript
145+
146+
```typescript
147+
function hasCycle(head: ListNode | null): boolean {
148+
let slowNode: ListNode | null = head,
149+
fastNode: ListNode | null = head;
150+
while (fastNode !== null && fastNode.next !== null) {
151+
slowNode = slowNode!.next;
152+
fastNode = fastNode.next.next;
153+
if (slowNode === fastNode) return true;
154+
}
155+
return false;
156+
};
157+
```
158+
159+
160+
142161

143162
-----------------------
144163
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /