刷题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* head){
            if(head==NULL || head->next==NULL){
                return NULL;
            }
            unordered_map<ListNode*,int> dp;
            dp[head] = 1;
            while(head!=NULL && dp[head]<2){
                head = head->next;
                if(dp.count(head)>0){
                    dp[head]++;
                }
            }
            return head;
        }
};

性能:

Runtime: 20 ms, faster than 24.70% of C++ online submissions for Linked List Cycle II.
Memory Usage: 12.5 MB, less than 7.14% of C++ online submissions for Linked List Cycle II.

三、优化措施

不使用额外的空间,就要用上一个题目141. Linked List Cycle的fast,slow双指针法了。

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

                if(fast == slow){
                    slow = head;
                    while(fast!=slow){
                        fast = fast->next;
                        slow = slow->next;
                    }
                    return fast;
                }
            }

            return NULL;
        }
};

性能如下:

Runtime: 4 ms, faster than 99.87% of C++ online submissions for Linked List Cycle II.
Memory Usage: 9.9 MB, less than 59.52% of C++ online submissions for Linked List Cycle II.

原文地址:https://www.cnblogs.com/siweihz/p/12273097.html

时间: 2024-10-09 19:15:29

刷题142. Linked List Cycle II的相关文章

141. Linked List Cycle &amp;&amp; 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

Java for 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? 解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II JAVA实现如下: public ListNode detectCycle(Li

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 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? 同Leetcode 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

LeetCode刷题:Linked List Cycle 及其进阶Linked List Cycle II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 1. 首先解决的问题是 判断单链表 有没有环? 解题思路: 两个指针slow 和 fast,开始都指向 head, slow指针每次走一步,fast 指针每次走两步,看看最终 slow 和 fast 会不会重合,如果 链表有环,那么一定会重合. /** * Definition

leetcode 142. Linked List Cycle II ----- java

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? 141题的延伸,求出循环点. 可以用数学方法证明出slow与find相遇的位置一定是所求的点. /** * Definitio

[leedcode 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? 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!).  我们已经得到了结论a=c,那么让两个指针分别从X

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