Cover image for 面试经典150题 P141 环形链表

面试经典150题 P141 环形链表


时间轴

时间轴

2025-11-19

init

链表,双指针

题目:

双指针,快慢指针

1234567891011121314151617181920212223242526272829303132333435
struct ListNode {	int val;	ListNode *next;	ListNode(int x)		: val(x)		, next(nullptr)	{	}};class Solution {    public:	bool hasCycle(ListNode *head)	{		ListNode *low = head, *fast = head;		do {			if (low == nullptr || low->next == nullptr)				return false;			else				low = low->next;			if (fast == nullptr || fast->next == nullptr || fast->next->next == nullptr)				return false;			else				fast = fast->next->next;		} while (low != fast);		if (low != nullptr)			return true;		return false;	}};
评论加载中…