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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Leetcode 142. Linked List Cycle II

2019-11-11 06:35:35
字體:
供稿:網(wǎng)友

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; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 静海县| 宜兴市| 曲沃县| 宁阳县| 门源| 黑河市| 榆树市| 荔浦县| 天镇县| 永昌县| 策勒县| 东丽区| 高尔夫| 桑日县| 昭觉县| 富源县| 英超| 明星| 盐源县| 朝阳县| 博客| 湄潭县| 青铜峡市| 乐亭县| 鹿邑县| 家居| 丰原市| 克什克腾旗| 任丘市| 东海县| 本溪市| 邯郸市| 攀枝花市| 佛坪县| 德州市| 同德县| 扎兰屯市| 西青区| 西吉县| 高平市| 赞皇县|