Leedcode9-linked-list-cycle-i
生活随笔
收集整理的這篇文章主要介紹了
Leedcode9-linked-list-cycle-i
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
判斷鏈表是不是循環(huán)鏈表
#include<iostream> #include<vector> using namespace std; /* Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space?*/ struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {} }; class Solution { //我的思路 錯(cuò)誤 public:bool hasCycle(ListNode *head) {ListNode* faster = head;ListNode* slower = head;while (faster->next->next != NULL && faster->next != NULL) {faster = faster->next->next;slower = slower->next;}if (faster==slower)return true;return false;} }; class Solution { public:bool hasCycle(ListNode *head) {if (head == NULL)return false;ListNode* faster = head;ListNode* slower = head;while(faster->next->next != NULL && faster->next != NULL) {slower = slower->next;faster = faster->next->next;if (faster == slower)return true;}return false;} };?
總結(jié)
以上是生活随笔為你收集整理的Leedcode9-linked-list-cycle-i的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL-queue.back()队尾误区
- 下一篇: ilitek win10 触摸屏驱动_想