【LeetCode每天一题】Linked List Cycle II(循环链表II)

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-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

思路



  这道题的主要思路就是我们先判断链表是否存在环(快慢指针快指针每次移动两步,慢指针每次移动一步,如果存在环的话,两个指针会重合),然后找出这个环一共有几个节点(从重合的节点开始遍历一圈得到环中的节点数),最后从头开始设置快慢指针,快指针先移动环的节点数步,然后快慢指针一起移动。当快慢指针重合时,指向的节点就表示环的入口节点。时间复杂度未O(n), 空间复杂度为O(1)。

解决代码


 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def detectCycle(self, head):
 9         """
10         :type head: ListNode
11         :rtype: ListNode
12         """
13         if not head or not head.next:
14             return None
15         one, two = head, head.next       # 设置快慢指针
16         while one is not two:            # 判断是否存在环
17             if not two or not two.next:
18                 return None
19             one = one.next
20             two = two.next.next
21
22         count = 1             # 记录环中节点个数
23         while one is not two.next:
24             two = two.next
25             count += 1
26
27         one, two = head, head      # 重新指向头节点
28         while count > 0:          # two指针先移动count次
29             two = two.next
30             count -= 1
31         while one is not  two:      # 然后one,two指针一起移动。
32             one = one.next
33             two = two.next
34         return one           # 返回one指向的节点就为环的入口节点。       

原文地址:https://www.cnblogs.com/GoodRnne/p/10957057.html

时间: 2024-11-08 22:20:06

【LeetCode每天一题】Linked List Cycle II(循环链表II)的相关文章

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 && 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 =

(LeetCode 141/142)Linked List Cycle

1.Given a linked list, determine if it has a cycle in it. 2.Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 3.Given a linked list, return the length of the cycle, if there is no cycle, return 0; 题目要求: 1

LeetCode【141】Linked List Cycle

Given a linked list, determine if it has a cycle in it. 快慢步,直接AC代码: 值得注意一下的地方就是if(!p2->next || !p2->next->next),如果p2==NULL,那么p2->next用法是错误的,而||运算符的性质是当前一个条件为真时,第二个条件不再判断. bool hasCycle(ListNode *head) { if(!head || !head->next) return false

[LC]141题 Linked List Cycle (环形链表)(链表)

①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入:head = [3,2,0,-4], pos = 1输出:true解释:链表中有一个环,其尾部连接到第二个节点. 示例 2: 输入:head = [1,2], pos = 0输出:true解释:链表中有一个环,其尾部连接到第一个节点. 示例 3: 输入:head = [1], pos =

leetcode_141题——Linked List Cycle (set)

这道题,将链表从前往后遍历,将前面遍历过的结点放入set中,依次往后,判断是否指向set中的结点即可 #include<iostream> #include<set> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; bool hasCycle(ListNode *head) { set<ListNode*>

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刷题笔记】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? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,

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