leetcode || 141、Linked List Cycle

problem:

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

Follow up:

Can you solve it without using extra space?

Hide Tags

Linked List Two
Pointers

thinking:

(1)如果可以开设额外的空间,使用unordered_set存储遍历过的结点,出现重复时即为存在环形结构

(2)如果不适用额外的空间,及空间复杂度为O(1),这里使用快、慢双指针。慢指针每次走一步,快指针每次走两步。

如果存在环形结构,两个指针总会相遇。

(3)终止条件也要注意:

fast!=NULL && fast->next!=NULL

防止出现fast->NULL->next

code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
          ListNode *fast,*slow;
         if(head==NULL) return false;
         slow=head;
         fast=head->next;
         while(fast!=NULL && fast->next!=NULL)
         {
             if(slow==fast) return true;
             slow=slow->next;
             fast=fast->next->next;
         }
         return false;
    }
};
时间: 2024-08-09 23:39:33

leetcode || 141、Linked List Cycle的相关文章

LeetCode 141, 142. Linked List Cycle I+II

判断链表有没有环,用Floyd Cycle Detection算法,用两个快慢指针. class Solution { public: bool hasCycle(ListNode *head) { if (!head) return false; ListNode *slow, *fast; slow=fast=head; do{ if (fast==NULL || fast->next==NULL) return false; slow = slow->next; fast = fast-

leetcode || 142、Linked List Cycle II

problem: 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? Hide Tags Linked List Two Pointers 题意:如果一个单链表存在环形结构,返回环的起点.空间复杂度满足O(1) thinking: (1)上一题使用快

leetcode 141 142. Linked List Cycle

题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*一个指针走的快 一个指针走得慢*/ class Solution { public: bool hasCycle(ListNode *head) { if(head ==

Leetcode 线性表 Linked List Cycle

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle Total Accepted: 17041 Total Submissions: 48975 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否有环 思路:快慢

LeetCode解题报告:Linked List Cycle && Linked List Cycle II

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 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

leetcode:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50412899 题目地址:https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return

leetcode笔记: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? 二. 题目分析 在Linked List Cycle题目中,使用了两个指针fast与slow检查链表是否有环,该题在此基础上,要求给出链表中环的入口位置,同样需要注意空间复杂度.为了

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