leetcode || 142、Linked List Cycle II

problem:

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?

Hide Tags

Linked List Two
Pointers

题意:如果一个单链表存在环形结构,返回环的起点。空间复杂度满足O(1)

thinking:

(1)上一题使用快慢指针可以判断是否存在环形结构,当fast==slow时,他俩指向的结点是环形结点的任意一个,与环的大小和位置有关。

(2)改造如下:慢指针和快指针都从头节点开始,慢指针每次走一步,快指针每次走两步,当slow==fast时,fast不变,slow回到起点head处,

此时快慢指针每次都走一步,再次相等时,两指针指向的节点即为环形的起点,也为终点。

举例证明: 1->2->3->2, 3指向前一个节点2

当快慢指针第一次相遇时,同时指向3,将慢指针重置到1,每次前进一步,再次相遇时,两个指针同时指向2,即为环形起点

(3)细节:快慢指针都从head开始,起点要一致。

code:

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
       if(head==NULL)
           return head;
       ListNode *slow=head;
       ListNode *fast=head;
       while(fast->next!=NULL && fast->next->next!=NULL)
       {
           slow=slow->next;
           fast=fast->next->next;
           if(slow==fast)
           {
               slow=head;
               while(slow!=fast)
               {
                   slow=slow->next;
                   fast=fast->next;
               }
               return slow;
           }
       }
       return NULL;
    }
};
时间: 2024-10-06 13:56:01

leetcode || 142、Linked List Cycle II的相关文章

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

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 day5 -- Reorder List && Linked List Cycle II

1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 分析:翻转链表是很常见的题目,这种翻转是第一次见.一开始

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

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

刷题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 II

/** * 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) { ListNode* fast = head; ListNode* slow = head;

LeetCode之“链表”:Linked List Cycle &amp;&amp; Linked List Cycle II

1. 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 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =