We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 15a283c commit 0b7820aCopy full SHA for 0b7820a
go/linked_list_cycle.go
@@ -2,7 +2,7 @@
2
package main
3
4
// `https://github.com/ryo-devz/LeetCode/pull/1#discussion_r1710718113`に書かれているとおり、印をつける方法はデメリットが大きい
5
-func hasCycle(head *ListNode) bool {
+func hasCycleFloyd(head *ListNode) bool {
6
slow, fast := head, head
7
for fast != nil && fast.Next != nil {
8
slow, fast = slow.Next, fast.Next.Next
@@ -12,3 +12,16 @@ func hasCycle(head *ListNode) bool {
12
}
13
return false
14
15
+
16
+func hasCycleMap(head *ListNode) bool {
17
+ seen := make(map[*ListNode]struct{})
18
+ node := head
19
+ for node != nil {
20
+ if _, ok := seen[node]; ok {
21
+ return true
22
+ }
23
+ seen[node] = struct{}{}
24
+ node = node.Next
25
26
+ return false
27
+}
0 commit comments