Skip to content

Commit 490abd7

Browse files
committed
go - linked list cycle
1 parent 263e2a4 commit 490abd7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

go/linked-list-cycle.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* type ListNode struct {
6+
* Val int
7+
* Next *ListNode
8+
* }
9+
*/
10+
11+
// https://leetcode.com/problems/linked-list-cycle/
12+
func hasCycle(head *ListNode) bool {
13+
if head == nil || head.Next == nil || head.Next.Next == nil {
14+
return false
15+
}
16+
17+
slow := head
18+
fast := head.Next.Next
19+
20+
for fast.Next != nil && fast.Next.Next != nil {
21+
if slow == fast {
22+
return true
23+
}
24+
slow = slow.Next
25+
fast = fast.Next.Next
26+
}
27+
28+
return false
29+
}

0 commit comments

Comments
 (0)