**一. 题目描述**
Given a linked list, determine if it has a cycle in it.
Follow up: Can you solve it without using extra space?
**二. 题目分析**
题目的意思是给定一个链表的头指针,快速判断一个链表是不是有环,如果有环,返回环的起始位置。该题的经典做法是使用两个指针,两个指针均指向头结点,其中一个是快指针,一次走两步;另一个是慢指针,一次只走一步,当两个指针相遇时,证明有环。这种方法的时间复杂度为`O(n)`,空间复杂度`O(1)`,这里需要考虑一些特殊情况:
* 空链表无环
* 链表只有一个节点时可能构成自环
**三. 示例代码**
~~~
#include <iostream>
struct ListNode
{
int value;
ListNode* next;
ListNode(int x) :value(x), next(NULL){}
};
class Solution
{
public:
bool hasCycle(ListNode *head)
{
if (head == nullptr || head->next == nullptr)
return false;
ListNode* fast = head;
ListNode* slow = head;
while (fast->next->next)
{
fast = fast->next->next;
slow = slow->next;
if (fast == slow) return true;
}
return false;
}
};
~~~
链表只有一个节点且该节点构成自环:
![](https://box.kancloud.cn/2016-01-05_568bb5eda469a.jpg)
链表3->4->5->6->7,4->5->6->7形成环:
![](https://box.kancloud.cn/2016-01-05_568bb5edb53e8.jpg)
**四. 小结**
关于有环链表中快慢指针一定会相遇的解决方法,可以简单地证明:
如果有环的话,快慢指针都会进入有环的部分。
而一旦进入有环的部分,一快一慢,学过物理都知道,其实可以相当于一个静止另一个每次移动一格。
到此,为什么一定会相遇应该已经很明显了吧~
该方法广为人知,不知是否有更为精妙的解法?
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching