Leetcode 141题 环形链表(Linked List Cycle) Java语言求解

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

Map集合解法

思路:
创建一个map集合,key为节点,value为地址值,因为ListNode没有重写toString()方法,所以用toString()方法返回的内容作为value。
如果map中存在当前节点的toString()方法返回的内容,则存在环。

/**
 * 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,String> map = new HashMap<ListNode,String>();
        while(head != null){
            if(map.containsValue(head.toString())){
                return true;
            }
            else{
                map.put(head,head.toString());
                head = head.next;
            }
        }
        return false;
    }
}

提交结果截图:

快慢指针解法


分析:
将slow指针指向head;
将fast指针指向head.next;
在循环的过程中slow走一步,fast走两步,如果存在环,则slow和fast一定会相遇,如本例子中:slow在1处,fast在2处;然后slow走一步,到2处,fast走两步,到4处;slow到3处,fast到3处,slow和fast相遇。
代码如下:

/**
 * 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;
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast != null){
            if(slow == fast){
                return true;
            }
            slow = slow.next;
            fast = fast.next;
            if(fast != null)
                fast = fast.next;
        }
        return false;
    }
}

对代码的解释:
1、如果head为空,则不是环,返回false;
2、如果fast不为空,则进入循环体;否则退出循环,无环,返回false;
3、如果slow和fast指向的节点相同,则存在环,返回true;
4、slow向后移动一个节点;
4、fast向后移动一个节点,如果fast不为空,再向后移动一个节点(不能直接移动两个节点,否则会报空指针异常);转2。
提交结果截图:


由图可知,快慢指针方法更好一些。

欢迎关注

扫下方二维码即可关注:

原文地址:https://www.cnblogs.com/nicaicai/p/12231849.html

时间: 2024-07-30 22:57:53

Leetcode 141题 环形链表(Linked List Cycle) Java语言求解的相关文章

LeetCode 141:环形链表 Linked List Cycle

给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-i

【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挪动到链表起始,

Leetcode 142题 环形链表 II(Linked List Cycle II) Java语言求解

题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 说明:不允许修改给定的链表. 分析 给出示意图: 对符号的一些说明: 公式演算: 很容易得到: m=x+y(环的长度两种表示形式); 快指针走两步,慢指针走一步.所以快指针的速度是慢指针的速度的二倍,所以相同时间内,快指针走的长度也是慢指针走的长度的二倍: 有:

141. 环形链表(Linked List Cycle)

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

Leetcode 206题 反转链表(Reverse Linked List)Java语言求解

题目描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 迭代解法 /** Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } */ class Solution { public ListNode

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode 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-indexe

【LeetCode】【Python】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? 思路:笨办法是每个节点再开辟一个属性存放是否访问过,这样遍历一遍即可知道是否有环.但为了不增加额外的空间,可以设置两个指针,一个一次走一步,另一个一次走两步,如果有环则两个指针一定会再次相遇,反之则不会. # Definition for singly-linked list.

LeetCode刷题总结-链表

LeetCode刷题总结-链表 一.链表     链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域.这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值. 表元素域elem用来存放具体的数据: 链接域next用来存放下一个节点的位置(python中的标识): 变量p指向链表的头节点(首节点)的位置,从p出发能找到表

LeetCode 142:环形链表 II Linked List Cycle II

给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 说明:不允许修改给定的链表. Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in th