1、题目描述
2、在VS2019上运行
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {
}
};
class Solution {
public:
// 判断链表是否有环,返回相遇的地方
ListNode* hasCycle(ListNode* head) {
if (head == NULL)
return NULL;
ListNode* fast = head;
ListNode* slow = head;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next;