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

首頁 > 學院 > 開發設計 > 正文

Leetcode 142. Linked List Cycle II

2019-11-11 07:52:41
字體:
來源:轉載
供稿:網友

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up: Can you solve it without using extra space?

s思路: 1. 如何用快慢指針大法檢測cycle,在Leetcode 141. Linked List Cycle以及說清楚了。不說了,直接來!

//方法1:快慢指針移動大法!class Solution {public: ListNode *detectCycle(ListNode *head) { // if(!head) return NULL; ListNode* fast=head->next,*slow=head; while(fast&&fast!=slow){ fast=fast->next?fast->next->next:NULL; slow=slow->next; } if(fast==NULL) return NULL; fast=head; slow=slow->next;//這里有一個bug:把fast放在頭部,那么slow需要往下移動一位,才開始同步移動! while(fast!=slow){ fast=fast->next; slow=slow->next; } return fast; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 大新县| 苗栗市| 敦化市| 得荣县| 延津县| 绥芬河市| 遂平县| 福贡县| 天祝| 漳浦县| 万州区| 佛学| 五大连池市| 山丹县| 贵南县| 广元市| 旺苍县| 龙口市| 蓬溪县| 宝应县| 梁平县| 岳阳市| 观塘区| 神农架林区| 宜宾县| 合肥市| 汝州市| 茌平县| 浮梁县| 马山县| 榕江县| 汕尾市| 红原县| 思南县| 阿坝县| 平武县| 山西省| 芦山县| 德惠市| 扶沟县|