linked-list-cycle-ii leetcode C++
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++的更多相关文章
- ★ Linked List Cycle II -- LeetCode
证明单链表有环路: 本文所用的算法 能够 形象的比喻就是在操场其中跑步.速度快的会把速度慢的扣圈 能够证明,p2追赶上p1的时候.p1一定还没有走完一遍环路,p2也不会跨越p1多圈才追上 我们能够 ...
- Linked List Cycle II || LeetCode
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 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 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 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 ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- [算法][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 ...
随机推荐
- Linux 文本相关命令(1)
Linux 文本相关命令(1) 前言 最近线上环境(Windows Server)出现了一些问题,需要分析一下日志.感觉 Windows 下缺少了一些 Linux 系统中的小工具,像在这波操作中用到的 ...
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- Java空指针异常:java.lang.NullPointerException解决办法
问题描述:运行maven项目抛出NullPointerException 空指针异常. 报空指针异常的原因有以下几种: 1字符串变量未初始化 例如:String x=null:对象x为null, ...
- Charles的breakpoint功能
修改请求报文 比如,前端已经控制了输入内容,而我们需要验证接口是否做了校验,这时候怎么测试? 可以通过charles抓包,修改请求报文,修改为在页面上无法输入的内容,发出去然后看后台怎么处理. 修改返 ...
- Python简单算法的实现
#coding=utf-8 #!/usr/bin/python def GCD(a,b): """ :求两个数的最大公约数 :param a: :param b: :re ...
- HTML 网页开发、CSS 基础语法——二.互联网原理
1. 互联网的运行过程 ①用户通过输入网址,发送一个HTTP请求到服务器中去,服务器里面存储了程序员上传的所有网页文件. ② 服务器一旦接收到请求,就会将我们所有的相关网页文件,回传到客户端,通过HT ...
- P6800-[模板]Chirp Z-Transform【NTT】
正题 题目链接:https://www.luogu.com.cn/problem/P6800 题目大意 给出一个\(n\)此多项式\(P\),对于\(k\in[0,m-1]\)所有的求\(P(c^k) ...
- 关于java实体类时间类型的格式化调整问题
关于java bean在后台\转化为json交给前台时间类型格式调整的方法: 首先要引入fastjson依赖. 在实体类上使用注解: @JsonFormat(pattern = "yyyy- ...
- python测试开发工具库汇总(转载)
Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...
- Java - 你的 Java 代码有这些坏味道吗?
列举一些 Java 开发中常见的"不良实践",来源于代码扫描(https://github.com/pmd/pmd),和诸君一起学习参考: 1 - 关闭资源 CloseResour ...