mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
Update solution 0141
This commit is contained in:
@ -30,12 +30,12 @@ func Test_Problem141(t *testing.T) {
|
||||
|
||||
{
|
||||
para141{[]int{3, 2, 0, -4}},
|
||||
ans141{true},
|
||||
ans141{false},
|
||||
},
|
||||
|
||||
{
|
||||
para141{[]int{1, 2}},
|
||||
ans141{true},
|
||||
ans141{false},
|
||||
},
|
||||
|
||||
{
|
||||
|
@ -23,19 +23,25 @@ Can you solve it without using extra space?
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* struct ListNode {
|
||||
* int val;
|
||||
* ListNode *next;
|
||||
* ListNode(int x) : val(x), next(NULL) {}
|
||||
* };
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func hasCycle(head *ListNode) bool {
|
||||
fast := head
|
||||
slow := head
|
||||
for slow != nil && fast != nil && fast.Next != nil {
|
||||
for fast != nil && fast.Next != nil {
|
||||
fast = fast.Next.Next
|
||||
slow = slow.Next
|
||||
if fast == slow {
|
||||
@ -45,6 +51,7 @@ func hasCycle(head *ListNode) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user