141 Linked List Cycle(判断链表是否有环Medium)

题目意思:链表有环,返回true,否则返回false

思路:两个指针,一快一慢,能相遇则有环,为空了没环

  ps:很多链表的题目:都可以采用这种思路

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     bool hasCycle(ListNode *head) {
12         if(head==NULL)return false;
13         ListNode *p1,*p2;
14         p1=p2=head;
15         while(p2->next&&p2->next->next){      //要判断p->next->next为空先要判断p->next是否为空,以免产生p->next->next不存在的蛋疼问题
16             p1=p1->next;
17             p2=p2->next->next;
18             if(p1==p2)
19                 return true;
20         }
21         return false;
22     }
23 };
时间: 2024-10-06 13:56:02

141 Linked List Cycle(判断链表是否有环Medium)的相关文章

LeetCode 141. 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) { ListNode* fast = head; ListNode

[leetcode]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? 题意: 给定一个链表,判断是否有环 思路: 快慢指针 若有环,则快慢指针一定会在某个节点相遇(此处省略证明) 代码: 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 ListNode f

141. Linked List Cycle 判断链表是否有环

class Solution { public: bool hasCycle(ListNode *head) { if(head == NULL) return false; if(head->next == NULL) return false; ListNode* p1 = head; ListNode* p2 = head; p1 = p1->next; p2 = p2->next->next; while(p2 != NULL && p2->next

[CareerCup] 2.6 Linked List Cycle 单链表中的环

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

141 Linked List Cycle 环形链表

给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solut

[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

[LintCode] Linked List Cycle 单链表中的环

Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail connects to node index 1, return true Challenge Follow up:Can you solve it without using extra space? s

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

判断链表是否有环及环入口点的求法(Linked List Cycle II )

分为两步 第一步 还是利用快慢指针,如果有环的话在利用快慢指针终会相会于一个节点. 第二步.然后从这节点出发每次出发走一步,同时从根节点出发每次出发也走一步则他们两个指针相遇的地方就是环的入口. 第一步好解释那么第二步是为什么呢? 网上有很多解法大都是从数学的角度来分析,有公式也有推算很不直观,我从图形的角度来看的话就相对理解起来简单很多. 将图摊开成一条线,假设我们有环而且假设快指针就多走了一圈就与慢指针相遇了(多走n圈其实也是一样的,画出图来也不难理解,只是画起来麻烦索性就以一圈来代表) 红