Update solution 0141

This commit is contained in:
halfrost
2021-07-17 21:21:21 +08:00
parent 23beddc2b1
commit 765bb777e3
2 changed files with 15 additions and 8 deletions

View File

@ -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},
},
{

View File

@ -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
}
```