Merge pull request #157 from brenobaptista/cleaned-141

Cleaned 141
This commit is contained in:
halfrost
2021-08-06 21:49:24 +08:00
committed by GitHub
2 changed files with 54 additions and 7 deletions

View File

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

View File

@ -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")
}