前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 141. Linked List Cycle题目Approach #1 (Two Pointers)代码Approach #2 (Hash Table)

LeetCode 141. Linked List Cycle题目Approach #1 (Two Pointers)代码Approach #2 (Hash Table)

作者头像
desperate633
发布2018-08-22 11:35:22
2340
发布2018-08-22 11:35:22
举报
文章被收录于专栏:desperate633

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?

题目

给定一个链表,判断它是否有环。

Approach #1 (Two Pointers)

使用两个指针slow,fast。两个指针都从表头开始走,slow每次走一步,fast每次走两步,如果fast遇到null,则说明没有环,返回false;如果slow==fast,说明有环,并且此时fast超了slow一圈,返回true。

为什么有环的情况下二者一定会相遇呢?因为fast先进入环,在slow进入之后,如果把slow看作在前面,fast在后面每次循环都向slow靠近1,所以一定会相遇,而不会出现fast直接跳过slow的情况。

代码

代码语言:javascript
复制
/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @return: True if it has a cycle, or false
     */
    public boolean hasCycle(ListNode head) {  
        // write your code here
        if (head == null || head.next == null) {
            return false;
        }

        ListNode slow = head;
        ListNode fast = head;

    while (true) {
        if (fast == null || fast.next == null) {
            return false;    //遇到null了,说明不存在环
        }
        slow = slow.next;
        fast = fast.next.next;
        if (fast == slow) {
            return true;   //第一次相遇在Z点
        }
    }
    }
}

Approach #2 (Hash Table)

想法很简单,如果链表中有一个环,那么我们只需要检查是否有一个节点被重复访问过,这个自然可以想到使用哈希表。 我们依次访问所有的链表节点,并将其节点的引用放到哈希表中,如果当前节点为空,就说明,我们已经到达了链表的末尾,而且这时候链表没有环,如果当前访问的节点在哈希表中出现过,也就是说明链表中有一个环。

代码语言:javascript
复制
public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null)
            return false;
        
        Set<ListNode> nodeSeen = new HashSet<ListNode>();
        while(head != null) {
            if(!nodeSeen.contains(head))
                nodeSeen.add(head);
            else
                return true;
            head = head.next;
        }
        return false;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.03.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • Approach #1 (Two Pointers)
  • 代码
  • Approach #2 (Hash Table)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档