141.环形链表

xiaoxiao2022-06-11  25

给定一个链表,判断链表中是否有环。

进阶: 你能否不使用额外空间解决此题?

/**  * 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) {         ListNode *slow = head, *fast = head;         while (fast && fast->next)          {             slow = slow->next;             fast = fast->next->next;             if (slow == fast)                  return true;         }         return false;     } };

转载请注明原文地址: https://www.6miu.com/read-4930517.html

最新回复(0)