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?

解题分析:

大致思想就是设置两个指针,一个指针每次走两步,一个指针每次走一步,如果这两个指针碰头了,那么一定就存在环

可以类比两个人在环形操场跑步,同时出发,一个跑得快,一个跑得慢,如果跑得快的人追上跑得慢的人,那么跑得快的人相当于多跑了一整圈

wiki:http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare


class Solution {
public:
bool hasCycle(ListNode *head) {
if (head == nullptr) return false;
if (head->next == nullptr) return false;

ListNode* fast = head;
ListNode* slow = head;
while (fast != nullptr && fast->next != nullptr && slow != nullptr ) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
return true;
}
}
return false;
}
};

每个指针都需要判断为空情况

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?

解题分析:

在上题中,slow指针和fast指针如果碰头了,那么链表中一定存在环

在快慢指针碰头的那一刻,我们另外设置一个新指针start,start指针和slow指针每次循环都前进一步,这两个指针一定会在环的开始处相遇


class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == nullptr) return nullptr;
if (head->next == nullptr) return nullptr;

ListNode* fast = head;
ListNode* slow = head;
ListNode* start = head;

while (fast != nullptr && fast->next != nullptr && slow != nullptr) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
break;
}
}

if (slow == start) return start;
while (slow != nullptr && start != nullptr) {
slow = slow->next;
start = start->next;
if (slow == start) {
return start;
}
}
return nullptr;
}
};

注意:存在一种情况就是 slow指针和fast指针恰好在链表开头相遇了,所以在
slow和start指针一起循环之前,需要额外判断一下

Leetcode:Linked List Cycle 链表是否存在环,布布扣,bubuko.com

时间: 2024-12-25 06:42:03

Leetcode:Linked List Cycle 链表是否存在环的相关文章

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

LeetCode: Linked List Cycle [141]

[题目] Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? [题意] 判断一个单向链表是否有环 [思路] 维护两个指针p1和p2,p1每次向前移动一步,p2每次向前移动两步 如果p2能够追上p1,则说明链表中存在环 [代码] /** * Definition for singly-linked list. * struct L

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

[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 [Floyd's cycle-finding algorithm]

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 Two Pointers ''' Created on Nov 13, 2014 @author: ScottGu<[email protected], [email protected]> ''' # Definit

[Leetcode] Linked list cycle ii 判断链表是否有环

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up:Can you solve it without using extra space? 题意:给定链表,若是有环,则返回环开始的节点,没有则返回NULL 思路:题目分两步走,第一.判断是否有环,第二若是有,找到环的起始点.关于第一点,可以参考之前的博客 Linked list cycle.

[LeetCode] 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? 开始以为这道题只需要注意不使用额外空间即可,于是写了个时间复杂度为O(n^2)暴力搜索算法,如下: /** * Dumped, Time Limit Exceeded */ class Solution { public: bool hasCycle(ListNode *hea

[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? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

leetCode 141. Linked List Cycle 链表

141. 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.  * struct ListNode {  *     int va