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 d91e0f9

Browse files
committed
update 142
1 parent f0d7bf1 commit d91e0f9

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# 0142.linked-list-cycle-ii
3+
4+
```text
5+
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
6+
7+
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
8+
9+
说明:不允许修改给定的链表。
10+
11+
进阶:
12+
13+
你是否可以使用 O(1) 空间解决此题?
14+
15+
来源:力扣(LeetCode)
16+
链接:https://leetcode-cn.com/problems/linked-list-cycle-ii
17+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
18+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ii
2+
3+
import "github.com/itcuihao/leetcode-go/structure"
4+
5+
func Run() {
6+
7+
}
8+
9+
func detectCycle(head *structure.ListNode) *structure.ListNode {
10+
f, s := head, head
11+
isC := false
12+
for f != nil && f.Next != nil {
13+
s = s.Next
14+
f = f.Next.Next
15+
if s == f {
16+
isC = true
17+
break
18+
}
19+
}
20+
if !isC {
21+
return nil
22+
}
23+
s = head
24+
for s != f {
25+
f = f.Next
26+
s = s.Next
27+
}
28+
return f
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package ii
3+
4+
import "testing"
5+
6+
func TestRun(t *testing.T) {
7+
Run()
8+
}

0 commit comments

Comments
(0)

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