前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode刷题】T107-环形链表 II

【leetcode刷题】T107-环形链表 II

作者头像
木又AI帮
修改2019-07-18 10:33:33
3870
修改2019-07-18 10:33:33
举报
文章被收录于专栏:木又AI帮木又AI帮

【题目】

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

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

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

代码语言:javascript
复制
示例 :
输入:head = [,,,-4], pos = 
输出:tail connects to node index 
解释:链表中有一个环,其尾部连接到第二个节点。
代码语言:javascript
复制
示例 :
输入:head = [,], pos = 
输出:tail connects to node index 
解释:链表中有一个环,其尾部连接到第一个节点。
代码语言:javascript
复制
示例 :
输入:head = [], pos = -1
输出:no cycle
解释:链表中没有环。

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

【思路】

我们使用【T106-环形链表】的方法,找到first和second指针相遇点(first指针每次移动一步,second移动两步)。

如图所示,假设头结点到环入口的距离为a,环入口到相遇点的距离为b,环的长度为C。

那么first指针移动步数为a+b+nC

second指针移动步数为a+b+mC

并且有second指针移动步数是first的两倍,即a+b+mC = 2*(a+b+nC)

有a = (m-n)C - b = (m-n-1)C + (C-b)

因此,first指针重新指向头结点,second指针位置不变,两指针每次移动一步,那么再次相遇时,所在节点为环入口。

【代码】

python版本

代码语言:javascript
复制
# 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:
            return None
        # 2*(a+b+nC) = a+b+mC
        # ==> a = (m-n)C - b
        # 找到重复节点后,first指针指向头结点
        # first、second两指针都每次移动一步,相遇点即是环入口
        first = head
        second = head
        while True:
            if not second.next or not second.next.next:
                return None
            first = first.next
            second = second.next.next
            if first == second:
                break

        first = head
        while first != second:
            first = first.next
            second = second.next
        return first

C++版本

代码语言:javascript
复制
/**
 * 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)
            return NULL;
        // 2*(a+b+nC) = a+b+mC
        // ==> a = (m-n)C - b
        // 找到重复节点后,first指针指向头结点
        // first、second两指针都每次移动一步,相遇点即是环入口
        ListNode* first = head;
        ListNode* second = head;
        // 找到相遇点
        while(true){
            if(!second->next || !second->next->next)
                return NULL;
            first = first->next;
            second = second->next->next;
            if(first == second)
                break;
        }

        // first指向头结点,两指针每次移动一步
        first = head;
        while(first != second){
            first = first->next;
            second = second->next;
        }
        return first;
    }
};
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 木又AI帮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档