袋熊的树洞

日拱一卒,功不唐捐

0%

LeetCode 141 - Linked List Cycle

问题描述

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:

1
2
3
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:

1
2
3
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:

1
2
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?

Related Topics: Linked List, Two Pointers

原问题: 141. Linked List Cycle

中文翻译版: 141. 环形链表

解决方案

该题可以使用双指针方法进行解决,设定快指针 fast 和慢指针 slow,两指针同时从头节点 head 出发,慢指针每前进一个节点,快指针就前进两个节点,如果链表有环,由于两指针前进速度不同,最终两指针会汇聚在同一个节点,即 fast == slow,否则,快指针会最先到达链表节点,两指针不会汇聚在一起。

参考解题代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "List.h"
using namespace std;

/**
* 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)
return false;

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

return false;
}
};

int main()
{
ListNode *node1 = create_list_node(1);
ListNode *node2 = create_list_node(2);
ListNode *node3 = create_list_node(3);
ListNode *node4 = create_list_node(4);
connect_list_nodes(node1, node2);
connect_list_nodes(node2, node3);
connect_list_nodes(node3, node4);
connect_list_nodes(node4, node2);

Solution solu;
cout << solu.hasCycle(node1) << endl;

return 0;
}