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* slow = head;
        while(fast && fast -> next){
            fast = fast -> next -> next;
            slow = slow -> next;
            if(fast == slow) return true;
        }
        return false;
    }
};

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12331786.html

时间: 2024-08-29 13:06:58

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

[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(判断链表是否有环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 { 1

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

LeetCode | 0141. Linked List Cycle环形链表【Python】

LeetCode 0141. Linked List Cycle环形链表[Easy][Python][双指针] 题目 英文题目地址 Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked lis

[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

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

LeetCode(141): 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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)

判断cycle存在 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 本题要求判断给定链表中是否存在循环.并且题目要求不要使用extra space,因此,我们就不能保存每一个结点的value,在遍历的时候判断是否是循环.这道题可以通过使用速度不同的pointer进行遍历,若两个pointer相遇则说明存在cycle,若遇到N

[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