mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
30
leetcode/0141.Linked-List-Cycle/141. Linked List Cycle.go
Normal file
30
leetcode/0141.Linked-List-Cycle/141. Linked List Cycle.go
Normal file
@ -0,0 +1,30 @@
|
||||
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) {}
|
||||
* };
|
||||
*/
|
||||
|
||||
func hasCycle(head *ListNode) bool {
|
||||
fast := head
|
||||
slow := head
|
||||
for slow != nil && fast != nil && fast.Next != nil {
|
||||
fast = fast.Next.Next
|
||||
slow = slow.Next
|
||||
if fast == slow {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package leetcode
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_Problem141(t *testing.T) {
|
||||
}
|
18
leetcode/0141.Linked-List-Cycle/README.md
Normal file
18
leetcode/0141.Linked-List-Cycle/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a linked list, determine if it has a cycle in it.
|
||||
|
||||
Follow up:
|
||||
Can you solve it without using extra space?
|
||||
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
判断链表是否有环,不能使用额外的空间。
|
||||
|
||||
## 解题思路
|
||||
|
||||
给 2 个指针,一个指针是另外一个指针的下一个指针。快指针一次走 2 格,慢指针一次走 1 格。如果存在环,那么前一个指针一定会经过若干圈之后追上慢的指针。
|
Reference in New Issue
Block a user