LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)


Time:2019/4/7
Title: Linked List Cycle
Difficulty: Easy
Author:小鹿


题目:Linked List Cycle I

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-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
复制代码

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
复制代码

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
复制代码

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

Slove:
▉ 算法思路:

两种解题思路:

1)哈希表法:遍历链表,没遍历一个节点就要在哈希表中判断是否存在该结点,如果存在,则为环;否则,将该结点插入到哈希表中继续遍历。

2)用两个快慢指针,快指针走两步,慢指针走一步,如果快指针与慢指针重合了,则检测的当前链表为环;如果当前指针或下一指针为 null ,则链表不为环。

▉ 方法一:哈希表
   /**
        * Definition for singly-linked list.
        * function ListNode(val) {
        *     this.val = val;
        *     this.next = null;
        * }
        */
        /**
        * @param {ListNode} head
        * @return {boolean}
        */

        var hasCycle = function(head) {
            let fast = head;
            let map = new Map();
            while(fast !== null){
                if(map.has(fast)){
                    return true;
                }else{
                    map.set(fast);
                    fast = fast.next;
                }
            }
            return false;
        };

复制代码
▉ 方法二:快慢指针
 var hasCycle = function(head) {
     if(head == null || head.next == null){
     	return false;
     }
     let fast = head.next;
     let slow = head;
     while(slow != fast){
         if(fast == null || fast.next == null){
         	return false;
     	 }
         slow = slow.next;
         fast = fast.next.next;
     }
     return true;
 };
复制代码
▉ 方法二:快慢指针

这部分代码是我自己写的,和上边的快慢指针思路相同,运行结果相同,但是当运行在 leetcode 时,就会提示超出时间限制,仔细对比代码,我们可以发现,在逻辑顺序上还是存在差别的,之所以超出时间限制,是因为代码的运行耗时长。

//超出时间限制
var hasCycle = function(head) {
    if(head == null || head.next == null){
    	return false;
    }
    let fast = head.next;
    let slow = head;
    while(fast !== null && fast.next !== null){
        if(slow === fast) return true;
        slow = head.next;
        fast = fast.next.next;
    }
	return false;
};
复制代码


欢迎一起加入到 LeetCode 开源 Github 仓库,可以向 me 提交您其他语言的代码。在仓库上坚持和小伙伴们一起打卡,共同完善我们的开源小仓库! Github:https://github.com/luxiangqiang/JS-LeetCode
欢迎关注我个人公众号:「一个不甘平凡的码农」,记录了自己一路自学编程的故事。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值