本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
141.环形链表
来源:力扣(LeetCode)
给你一个链表的头节点 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, ] 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++实现
/**
* 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 == NULL || head->next == NULL)
return false;
unordered_map<ListNode*, int> hashTable;
while (head){
auto it = hashTable.find(head);
if ( it!= hashTable.end()){
return true;
}
hashTable[head] = 1;
head = head->next;
}
return false;
}
};
复杂度分析
- 时间复杂度:
- 空间复杂度: 哈希表存储占用空间
快慢指针
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++实现
/**
* 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 == 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;
}
};
/**
* 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 == 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;
}
};
复杂度分析
- 时间复杂度:
- 空间复杂度: