国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 温宿县| 古蔺县| 岑巩县| 临高县| 乌兰县| 郎溪县| 新闻| 宁化县| 微博| 许昌市| 丰都县| 花莲县| 海晏县| 沙坪坝区| 常德市| 五峰| 柳河县| 昆山市| 灵丘县| 巴中市| 新野县| 石台县| 萝北县| 沙河市| 东莞市| 云浮市| 虹口区| 蒙自县| 饶阳县| 五家渠市| 英吉沙县| 嵊州市| 凤阳县| 苏州市| 杂多县| 固始县| 汝南县| 宣恩县| 嘉善县| 寿光市| 满洲里市|