mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
@ -9,17 +9,16 @@ 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 {
|
||||
|
@ -1,6 +1,54 @@
|
||||
package leetcode
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question141 struct {
|
||||
para141
|
||||
ans141
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para141 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans141 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem141(t *testing.T) {
|
||||
|
||||
qs := []question141{
|
||||
|
||||
{
|
||||
para141{[]int{3, 2, 0, -4}},
|
||||
ans141{true},
|
||||
},
|
||||
|
||||
{
|
||||
para141{[]int{1, 2}},
|
||||
ans141{true},
|
||||
},
|
||||
|
||||
{
|
||||
para141{[]int{1}},
|
||||
ans141{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 141------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans141, q.para141
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, hasCycle(structures.Ints2List(p.one)))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
|
Reference in New Issue
Block a user