国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

leecode 解題總結:141. Linked List Cycle

2019-11-08 02:49:07
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>#include <string>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?16:48~16:48,16:54完成分析:分析一個鏈表是否有環(huán),可以通過設置快慢指針的方式,快指針每次走兩步,滿指針每次走一步,如果某一時刻,快指針=慢指針,則存在環(huán)*/struct ListNode {   int val;   ListNode *next;   ListNode(int x) : val(x), next(NULL) {}};class Solution {public:    bool hasCycle(ListNode *head) {        if(!head)		{			return false;		}		ListNode* slow = head;		ListNode* fast = head->next;		bool isFind = false;		while(fast && slow)		{			if(slow == fast)			{				isFind = true;				break;			}			slow = slow->next;			if(fast->next)			{				fast = fast->next->next;			}			//說明快指針下一個指針為空,鏈表已經遍歷完了,不可能有環(huán)			else			{				break;			}		}		return isFind;    }};void PRint(vector<int>& result){	if(result.empty())	{		cout << "no result" << endl;		return;	}	int size = result.size();	for(int i = 0 ; i < size ; i++)	{		cout << result.at(i) << " " ;	}	cout << endl;}void process(){	 vector<int> nums;	 int value;	 int num;	 Solution solution;	 vector<int> result;	 while(cin >> num )	 {		 nums.clear();		 for(int i = 0 ; i < num ; i++)		 {			 cin >> value;			 nums.push_back(value);		 }	 }}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪雅县| 资溪县| 遂宁市| 图片| 大城县| 鹰潭市| 景泰县| 连城县| 自治县| 汉中市| 安多县| 上饶市| 永善县| 关岭| 玉环县| 南和县| 巩留县| 沧州市| 荣昌县| 七台河市| 德州市| 湖北省| 容城县| 陵水| 禹城市| 云阳县| 双辽市| 大城县| 栾川县| 建平县| 齐河县| 洛南县| 大连市| 江华| 乌海市| 台中市| 定兴县| 沂水县| 安图县| 大方县| 洛浦县|