栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

leetcode:141.环形链表

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode:141.环形链表

141.环形链表

来源:力扣(LeetCode)

链接: https://leetcode.cn/problems/linked-list-cycle/

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

如果链表中存在环 ,则返回 true 。 否则,返回 false 。

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。

提示:
链表中节点的数目范围是 [0, 1 0 4 10^4 104]
− 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 −105<=Node.val<=105
pos 为 -1 或者链表中的一个有效索引 。

解法
  • 遍历+哈希表:遍历,并用哈希表存储该节点,遍历每个节点,判断该节点是否在哈希表中,如果在的话,表明有环;如果遍历到最后,说明无环;
  • 快慢指针:指定快慢两个指针,慢指针每次走一步,快指针每次走两步,类似于龟兔赛跑,如果存在环路,快指针最终肯定能追上慢指针,二者重合;如果不存在环路,快指针遍历到最后即可结束;这里快慢指针可以是从相同地方起步,也可以是快指针在慢指针的下一个位置,注意这二者的while循环条件;
代码实现 遍历+哈希表

python实现

 # Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # 暴力+ 哈希法
        s = dict()
        if not head or not head.next:
            return False
 
        while head:
            if head in s:
                return True
            s[head] = ''
            head = head.next
        return False

c++实现

class Solution {
public:
    bool hasCycle(ListNode *head) {
        // 暴力法+哈希表
        if (head == NULL || head->next == NULL)
            return false;
        
        unordered_map hashTable;
        while (head){
            auto it = hashTable.find(head);
            if ( it!= hashTable.end()){
                return true;
            }
            hashTable[head] = 1;
            head = head->next;
        }
        return false;
    }
};

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n) 哈希表存储占用空间
快慢指针

python实现

 # Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # 快慢指针,一个走一步,一个走两步,如果存在环路,是会相交的
        if not head or not head.next:
            return False
        slow = head
        fast = head.next
        while slow != fast:
            if not fast or not fast.next:
                return  False
            slow = slow.next
            fast = fast.next.next
        return True
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # 快慢指针,一个走一步,一个走两步,如果存在环路,是会相交的
        if not head or not head.next:
            return False
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

c++实现

class Solution {
public:
    bool hasCycle(ListNode *head) {
       if (head == NULL || head->next == NULL){
           return false;
       } 

        ListNode* slow = head;
        ListNode* fast = head->next;
        while (slow != fast){
            if (fast == NULL || fast->next == NULL){
                return false;
            }
            slow = slow->next;
            fast = fast->next->next;
        }
        return true;

    }
};
class Solution {
public:
    bool hasCycle(ListNode *head) {
       if (head == NULL || head->next == NULL){
           return false;
       } 

        ListNode* slow = head;
        ListNode* fast = head;
        while (fast and fast->next){
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast)
                return true;
        }
        return false;

    }
};

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号