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

2020年1月29日21:23:19算法 LeetCode评论56阅读模式

题目描述: 给定一个链表,判断链表中是否有环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。文章源自随机的未知-https://sjdwz.com/11114.html

Map集合解法

思路: 创建一个map集合,key为节点,value为地址值,因为ListNode没有重写toString()方法,所以用toString()方法返回的内容作为value。 如果map中存在当前节点的toString()方法返回的内容,则存在环。文章源自随机的未知-https://sjdwz.com/11114.html

/**
 * 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;
    }
}

提交结果截图: Leetcode 141题 环形链表(Linked List Cycle) Java语言求解 Leetcode 141题 环形链表(Linked List Cycle) Java语言求解文章源自随机的未知-https://sjdwz.com/11114.html

快慢指针解法

Leetcode 141题 环形链表(Linked List Cycle) Java语言求解 分析: 将slow指针指向head; 将fast指针指向head.next; 在循环的过程中slow走一步,fast走两步,如果存在环,则slow和fast一定会相遇,如本例子中:slow在1处,fast在2处;然后slow走一步,到2处,fast走两步,到4处;slow到3处,fast到3处,slow和fast相遇。 代码如下:文章源自随机的未知-https://sjdwz.com/11114.html

/**
 * 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。 提交结果截图: Leetcode 141题 环形链表(Linked List Cycle) Java语言求解 Leetcode 141题 环形链表(Linked List Cycle) Java语言求解 由图可知,快慢指针方法更好一些。文章源自随机的未知-https://sjdwz.com/11114.html

欢迎关注

扫下方二维码即可关注: Leetcode 141题 环形链表(Linked List Cycle) Java语言求解文章源自随机的未知-https://sjdwz.com/11114.html

文章源自随机的未知-https://sjdwz.com/11114.html
欢迎关注本站微信公众号:随机的未知 如果喜欢本文,欢迎点赞,收藏,转发,打赏。
  • 本文由 发表于 2020年1月29日21:23:19
  • 转载请注明:来源:随机的未知 本文链接https://sjdwz.com/11114.html
算法

详解堆排序

什么是堆 堆首先是一个完全二叉树,堆分为大顶堆和小顶堆; 大顶堆 : 每个节点的值大于或等于其左右孩子节点的值,称为大顶堆。 小顶堆同理就是每个节点的值小于或等于其左右孩子节点的值。 注意: 每个节点...
算法

详解基数排序

基本思想 基数排序的思想是将整数按位数切割成不同的数字,然后按每个位数分别比较从而得到有序的序列。 例子 本文以数组中元素均为正整数来演示思想。 给定一个数组 arr = < 6, 56, 89 , ...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定