leetcode141. 环形链表

题目描述

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

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

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true

解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:true

解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入:head = [1], pos = -1
输出:false

解释:链表中没有环。

进阶:

你能用 O(1)(即,常量)内存解决此问题吗?

code

  • 通过修改链表
    • 时间复杂度o(n)
    • 空间复杂度o(1)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head)
            return 0;
        ListNode *p = head;
        int reset = 0x7f7f7f;
        while(p&&p->val!=reset){//判断p是否为NULL要写在前面
            p->val = reset;
            p = p->next;
        }
        return p ? 1 : 0;
    }
};
  • 不修改链表
  • 使用快慢指针:快指针每次前进2步,慢指针每次前进1步
    • 链表存在环时快慢指针一定会相遇?
      是的。
      • 由于快指针和慢指针的速度差是1步,而移动的最短距离也是1步,因此有环必相遇。
      • 也可以用反证法:
        假设链表存在环,而快慢二者不会相遇,则只可能出现一种情况:慢指针超越了快指针1步。慢指针往前推1步,快指针往前推2步,二者相遇,说明假设不成立。
    • 时间复杂度:o(n)
    • 空间复杂度:o(1)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head)
            return false;
        ListNode *fast = head->next;
        ListNode *slow = head;
        while(fast!=slow){
            if(!fast||!fast->next)//fast走到链表尾
            return false;
        fast = fast->next->next;
        slow = slow->next;
        }
        return true;
    }
};
  • 不修改链表
  • set存结点地址(唯一标识):使用的方法find
    • 时间复杂度:o(n),空间复杂度o(n)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        set<ListNode*> nodes;
        while(head){
            set<ListNode*>::iterator iter = nodes.find(head);
            if(iter!=nodes.end())
                return true;
            nodes.insert(head);
            head = head->next;
        }
        return false;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
  
class Solution {
public:
    bool hasCycle(ListNode *head) {
        unordered_set<ListNode*> nodes;
        while(head){
            unordered_set<ListNode*>::iterator iter = nodes.find(head);
            if(iter!=nodes.end())
                return true;
            nodes.insert(head);
            head = head->next;
        }
        return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值