LeetCode 142. Linked List Cycle II

简介: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

v2-4568f034b3b3cdc609ca366375546a2b_1440w.jpg

Description



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.


v2-4e350e7c616ab3919d5ba6df313d51ab_720w.jpg


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.


v2-5bd5714e7b79a360e97a4f04f63e5f67_720w.jpg


Example 3:

Input: head = [1], pos = -1

Output: no cycle

Explanation: There is no cycle in the linked list.


v2-faeeb9031a07904b5fb959efd28ee031_720w.png


Follow up:

Can you solve it without using extra space?


描述



给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。


说明:不允许修改给定的链表。


示例 1:

输入:head = [3,2,0,-4], pos = 1

输出:tail connects to node index 1

解释:链表中有一个环,其尾部连接到第二个节点。


示例 2:

输入:head = [1,2], pos = 0

输出:tail connects to node index 0

解释:链表中有一个环,其尾部连接到第一个节点。


示例 3:

输入:head = [1], pos = -1

输出:no cycle

解释:链表中没有环。


进阶:

你是否可以不用额外空间解决此题?


思路



  • 借助上一题的思路,我们使用两个指针,分别是slow和fast,slow指针每次向后走一个,fast指针每次向后走两步.
  • 此题目借鉴了求链表中倒数第k个节点的思想.
  • 基本思路是先求得环中一共有多少个节点m.
  • 然后slow,fast重置为head,将fast往后移动m-个节点,然后slow和fast同时往后移动,当fast.next == slow时返回slow即可.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-12 10:09:56
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-12 12:05:39
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return None
        count = 1
        slow, fast = head, head.next
        while fast and fast.next:
            # 找到第一次相遇的节点
            if fast == slow:
                # 确定环中节点的个数
                fast = fast.next
                while fast and fast.next:
                    # 如果fast指针和slow指针相遇,说明环已经走完一趟
                    # 结束循环
                    if fast == slow:
                        break
                    # 否则count自增一次
                    count += 1
                    slow, fast = slow.next, fast.next.next
                # 重置slow和fast只想首节点
                slow, fast = head, head
                # 将fast往后移count-1个(即环中节点个数少一个节点)
                for _ in range(count - 1):
                    fast = fast.next
                while fast:
                    # 当fast.next 指向slow时,说明已经到达结尾
                    if fast.next == slow:
                        return slow
                    fast = fast.next
                    slow = slow.next
                return slow
            slow, fast = slow.next, fast.next.next
        # 如果遍历的所有节点fast和slow都不等,说明没有环
        return None


源代码文件在这里.


目录
相关文章
|
6月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
21 1
|
6月前
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
28 0
|
6月前
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
21 0
|
1天前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
28 0
|
11月前
|
C++
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
54 0
|
11月前
|
C++
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
48 0
|
11月前
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
60 0
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
|
存储 C++
线性表的链式存储结构 单链表(Singly Linked List) C++
线性表的链式存储结构 单链表(Singly Linked List) C++
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle

热门文章

最新文章