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->next->next;
        }while(slow!=fast);
        return true;
    }
};

142是141的进阶,需要额外判断环的起点。

详见 https://leetcode.com/problems/linked-list-cycle-ii/solution/ 中的推导,不过里面应该是 F=(n-1)(a+b)+b,因此从head和相遇点开始同时走,一定会在循环起点相遇。

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if (!head) return NULL;
        ListNode *slow, *fast;
        slow = fast = head;
        do{
            if (!fast || !fast->next) return NULL;
            slow = slow->next;
            fast = fast->next->next;
        }while(slow!=fast);

        ListNode *p=head, *q=slow;
        while(p!=q){
            p = p->next;
            q = q->next;
        };
        return p;
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/9126249.html

时间: 2024-10-09 21:14:21

LeetCode 141, 142. Linked List Cycle I+II的相关文章

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: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 OJ 142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Subscribe to see which companies asked this question 解答: 先用快慢指针

【LeetCode】142 - 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? Solution: Discuss上的分析:Suppose the first meet at step k,the length of the Cycle is r. so..2k-k=nr,k=n

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),这里使用快.

141. Linked List Cycle && 142. Linked List Cycle II

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { pu

刷题142. Linked List Cycle II

一.题目说明 题目142. Linked List Cycle II,判断一个链表是否有环,如果有返回环的第一个元素,否则返回NULL. 这个题目是141. Linked List Cycle的升级版本,难度是Medium! 二.我的解答 最直观的解答就是用一个unordered_map<ListNode*,int> dp来统计节点出现的次数,如果出现2,则这个就是第一个节点. class Solution{ public: ListNode* detectCycle(ListNode* he

LeetCode解题报告:Linked List Cycle &amp;&amp; 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 线性表 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? 题意:判断一个链表中是否有环 思路:快慢