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 == NULL)
            return false;
        ListNode *first = head;
        ListNode *Second = head;
        while(Second->next != NULL){

            first = first->next;
            Second = Second->next->next;
            if(Second == NULL)
                return false;
            if(Second == first)
                return true;
        }
        return false;
    }
};

142  找到环开始的结点

第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。

因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)。

/**
 * 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) {
        if(head == NULL)
            return NULL;
        ListNode *first = head;
        ListNode *second = head;
        bool flag = true;
        while(second->next != NULL){
            first = first->next;
            second = second->next->next;
            if(second == NULL)
                return NULL;
            if(first == second){
                flag = false;
                break;
            }
        }
        if(flag == true)
            return NULL;
        first = head;
        while(first != second){
            first = first->next;
            second = second->next;
        }
        return first;

    }
};
时间: 2024-10-10 07:02:21

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

【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

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

本文为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 &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