leetcode每日一题(2020.10.09-10.10) 141. 环形链表(简单) 142. 环形链表 II(中等)


——————————————————————————————————————

141. 环形链表(简单)

哈希表

/**
 * 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) {
        if(head == null) return false;

        Map<ListNode, Integer> map = new HashMap<>();
        ListNode p = head;
        while(p != null){
            if(map.containsKey(p)){
                return true;
            }else{
                map.put(p, 1);
            }
            p = p.next;
        }
        return false;
    }
}

集合

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null) return false;

        Set<ListNode> set = new HashSet<>();
        ListNode p = head;
        while(p != null){
            if(set.add(p) == false){
                return true;
            }
            //官解优化:这一段可以去掉,因为上面已经添加过了
            /*else{
                set.add(p);
            }*/
            p = p.next;
        }
        return false;
    }
}

快慢指针(「Floyd 判圈算法」(又称龟兔赛跑算法))

1.假设有个哑结点,两者同时从哑结点出发

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null) return false;

        ListNode slow = head;
        ListNode fast = head.next;

        while(slow != fast){
            if(fast == null || fast.next == null) return false;
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;       
    }
}

2.没有哑结点,两者同时从头结点出发

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null) return false;

        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(中等)

快慢指针

只能用没有哑结点的形式,因为用了哑结点,链表中就多出来一个节点

/**
 * 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) {
        //检测有没有环,使用快慢指针slow(一次走一步)和fast(一次走两步)
        //if(head == null || head.next == null) return null;
        //这个地方不写也行

        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) break;
        }
        if(fast == null || fast.next == null) return null;

        //System.out.println("找到了" + slow.val);
        //找位置,当找到环之后,slow从head出发,fast从相遇点出发,一次都走一步,再次相遇为环的入口点
        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }  
        return slow;  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安安csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值