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

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

C++

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* detectCycle2(ListNode *head){
ListNode *fast = head;
ListNode *slow = head;
if(head==NULL) return NULL;
while (NULL != fast){
if (fast != NULL) fast = fast->next;
if (fast != NULL) fast = fast->next;
if (slow != NULL) slow = slow->next;
while(fast != NULL && slow == fast){
slow = head;
while(slow!=fast){
fast = fast->next;
slow = slow->next;
}
return slow;
}
}
return NULL;
} ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
if (NULL == head) return NULL;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if (fast != NULL && slow == fast)
return FindTheFirstSameNode(fast,head);
}
return NULL;
} ListNode *FindTheFirstSameNode(ListNode* head1, ListNode* head2){
while(head1 != head2){
head1 = head1->next;
head2 = head2->next;
}
return head1;
}
};

linked-list-cycle-ii leetcode C++的更多相关文章

  1. ★ Linked List Cycle II -- LeetCode

    证明单链表有环路: 本文所用的算法 能够 形象的比喻就是在操场其中跑步.速度快的会把速度慢的扣圈  能够证明,p2追赶上p1的时候.p1一定还没有走完一遍环路,p2也不会跨越p1多圈才追上  我们能够 ...

  2. Linked List Cycle II || LeetCode

    /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...

  3. Java for LeetCode 142 Linked List Cycle II

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

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  6. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  7. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  8. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  9. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  10. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

随机推荐

  1. Java数学函数的使用

    Java的Math类中提供了一系列关于数学运算的静态方法,常见的运算整理如下[1] 算数运算 Math.sqrt() // 平方根 Math.cbrt() // 立方根 Math.pow(a, b) ...

  2. 洛谷P1088——火星人(全排列+数学模拟)

    题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数字告诉人类科学 ...

  3. 关于buildroot移植的思考

    buildroot是一个成熟的SDK框架,基于它有了openwrt. 曾经有一个项目,需要将原有的OpenWrt SDK改造,并且将软件框架重新定义.尝试精简原来的OpenWrt,并且删除所有的软件包 ...

  4. ecshop增加调用字段问题汇总

    一.ecshop文章列表页调用缩略图.网页描述等 打开includes/lib_article.php文件,大约在69行 添加 $arr[$article_id]['description'] = $ ...

  5. Java面向对象系列(1)- 什么是面向对象

    面向过程 & 面向对象 面向过程思想 步骤清晰清楚,第一步做什么,第二步做什么-- 面对过程适合处理一些较为简单的问题 面向对象思想 物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分 ...

  6. windows 根据 端口号 找到进程ID PID

    List process by port number netstat -ano | findstr 8080 Proto Local Address Foreign Address State PI ...

  7. P4180-[BJWC2010]严格次小生成树【Kruskal,倍增】

    正题 题目链接:https://www.luogu.com.cn/problem/P4180 题目大意 \(n\)个点\(m\)条边的一张无向图,求它的严格次小生成树. \(1\leq n\leq 1 ...

  8. Python如何连接Mysql及基本操作

    什么要做python连接mysql,一般是解决什么问题的 做自动化测试时候,注册了一个新用户,产生了多余的数据,下次同一个账号就无法注册了,这种情况怎么办呢?自动化测试都有数据准备和数据清理的操作,如 ...

  9. cybersploit

    靶机准备 将ova文件导入虚拟机中,设置网络模式为NAT 从靶机描述得知包含3个flag kali扫描其ip netdiscover -r 192.168.164.0/24 渗透测试 namp扫描端口 ...

  10. nginx配置禁止爬虫配置

    1.在配置文件里添加禁止爬虫配置 server { ------ #添加如下内容即可防止爬虫 if ($http_user_agent ~* "qihoobot|Baiduspider|Go ...