【LeetCode】Linked List Cycle

Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

做完Linked List Cycle II在做这题简直就是阉割版。。

fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false


class Solution
{
public:
bool hasCycle(ListNode *head)
{
if(head == NULL)
return false;
ListNode* fast = head;
ListNode* slow = head;
do
{
if(fast->next && slow->next)
{
fast = fast->next;
slow = slow->next;
}
else
return false;

if(fast->next)
fast = fast->next;
else
return false;
}while(fast != slow);

return true;
}
};

【LeetCode】Linked List Cycle,布布扣,bubuko.com

时间: 2024-12-09 23:19:49

【LeetCode】Linked List Cycle的相关文章

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【leetcode】Linked List Cycle (python)

题目分析见这里 class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast = head pslow = head #找第一次相遇的点,若存在环路,则肯定会相遇 while pfast and pfast.next: pfast = pfast.next.nex

【leetcode】Linked List Cycle II (middle)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离

【LeetCode】【Python】Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 思路:笨办法是每个节点再开辟一个属性存放是否访问过,这样遍历一遍即可知道是否有环.但为了不增加额外的空间,可以设置两个指针,一个一次走一步,另一个一次走两步,如果有环则两个指针一定会再次相遇,反之则不会. # Definition for singly-linked list.

【LeetCode】【C++】Linked list cycle 2

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 此题不难,但想不用extra space做出来还是要动点脑筋的,因为我之前看过类似的算法题,所以就很快想到了. 方法是利用两个指针从头开始,指针p1一次走一步,指针p2一次走两步,如果有环则两指针必

LeetCode【141】Linked List Cycle

Given a linked list, determine if it has a cycle in it. 快慢步,直接AC代码: 值得注意一下的地方就是if(!p2->next || !p2->next->next),如果p2==NULL,那么p2->next用法是错误的,而||运算符的性质是当前一个条件为真时,第二个条件不再判断. bool hasCycle(ListNode *head) { if(!head || !head->next) return false

【leetcode82】Linked List Cycle

题目描述: 判断有序list是不是环 要求: 时间复杂度o(n) 原文描述: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 思路: 设置两个指针,一个快指针,每次走两步,一个慢指针,每次走一步 如果块指针==慢指针,那么包含环 注意判断空值和长度为一的情况 - 代码: /** * Definition for singly

【链表】Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. 思路: 对于判断链表是否有环,方法很简单,用两个指针,一开始都指向头结点,一个是快指针,一次走两步,一个是慢指针,一次只走一步,当两个指针重合时表示存在环了. fast先进入环,在slow进入之后,如果把slow看作在前面,fast在后面每次循环都向slow靠近1,所以一定会相遇,而不会出现fast直接跳过slow的情况. /** * Definition for singly

【Leetcode】Linked List Random Node

题目链接:https://leetcode.com/problems/linked-list-random-node/ 题目: Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely l