LeetCode141&142.环形链表

141 环形链表I

题目描述

题目链接
给定一个链表,判断链表中是否有环。

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

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
在这里插入图片描述
示例 2:

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
在这里插入图片描述
示例 3:

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
在这里插入图片描述
进阶:

你能用 O(1)(即,常量)内存解决此问题吗?

方法一 哈希表

思路

每遍历一个节点都写入HashSet,并在写入前查看该节点是否已经存在了HashSet中

Java代码

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<ListNode>();
        while(head != null){
            if(set.contains(head))
                return true;
            else
                set.add(head);
            head = head.next;
        }
        return false;
    }
}

方法2 快慢指针

思路

就像两个运动员在环形田径场跑步,跑得快的运动员必然会从后面再次与跑得慢的远动员相遇。
这里定义两个指针, i , j i,j i,j, i i i每次移动一步, j j j每次移动两步。

  • 当两个指针重合,就代表,链表有环;
  • 当快指针指向了null,说明链表无环。
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)
                return true;
        }
        return false;
    }
}

142 环形链表II

题目描述

题目链接
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 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
解释:链表中没有环。
在这里插入图片描述
进阶:
你是否可以不用额外空间解决此题?

方法一 哈希表

思路

上一题使用哈希表的方法,重复的节点即为入环点。

代码

public class Solution {
    public ListNode detectCycle(ListNode head) {
        Set<ListNode> visited = new HashSet<ListNode>();

        ListNode node = head;
        while (node != null) {
            if (visited.contains(node)) {
                return node;
            }
            visited.add(node);
            node = node.next;
        }

        return null;
    }
}

方法二 双指针法

思路

该题在上一题的基础上,增加了要求:求出入环点。
在这里插入图片描述

  • 从链表头到入环点的距离是D,
  • 从入环点到两个指针首次相遇点的距离是S1,
  • 从首次相遇点回到入环点的距离是S2。

当两个指针首次相遇时:

  • 慢指针slow每次只走一步,所走的距离是 D + S 1 D+S1 D+S1,
  • 快指针fast每次走两步,多走了一个圈,所走的距离是 D + S 1 + S 2 + S 1 = D + 2 S 1 + S 2 D+S1+S2+S1=D+2S1+S2 D+S1+S2+S1=D+2S1+S2

由于fast的速度是slow的两倍,那么fast所走的距离同样是slow所走距离的两倍,因此:
2 ( D + S 1 ) = D + 2 S 1 + S 2 2(D+S1)=D+2S1+S2 2(D+S1)=D+2S1+S2
-> D = S 2 D=S2 D=S2
那么在首次相遇时,将快指针移回链表头结点位置,两个指针每次向前走一步,最终相遇点就是环的入口点。

代码

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                fast = head;
                while(fast != slow){
                    fast = fast.next;
                    slow = slow.next;
                }
                return fast;
            }
                
        }
        return null;   
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值