53-Linked List Cycle II
- Linked List Cycle II   My Submissions QuestionEditorial Solution 
Total Accepted: 74093 Total Submissions: 235430 Difficulty: Medium
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?
思路:假设快慢指针在x处相遇, 
此时快指针在环内假设走了n圈加x步,而总的步数:nr+x+a 
是慢指针的2倍 
即有nr+x+a =(x+a)*2 
上式为a=(n-1)r+r-x; 
说明慢指针走到a时候,快指针从x处走了r-x前一个加上n-1圈的距离, 
注意此时快指针从X处一步一步走,慢指针也一步一步走,直到在入口相遇
时间复杂度:O(n) 
空间复杂度:O(1)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head==NULL||head->next==NULL)return NULL;
        ListNode *p_quick=head,*p_slow=head;
        while(p_quick!=NULL&&p_quick->next!=NULL){
            p_quick = p_quick->next->next;
            p_slow = p_slow->next;
            if(p_quick==p_slow)break;
        }
        if(p_quick==p_slow){
            p_slow=head;
            while(p_slow!=p_quick){
                p_slow = p_slow->next;
                p_quick=p_quick->next;
            }
            return p_slow;
        }
        else return NULL;
    }
};
53-Linked List Cycle II的更多相关文章
- [算法][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 ...
 - 15. 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 solve i ...
 - 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
		
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
 - [Linked List]Linked List Cycle,Linked List Cycle II
		
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
 - Linked List Cycle && Linked List Cycle II
		
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
 - LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
		
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
 - 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
		
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
 - 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 ...
 
随机推荐
- elasticsearch基于RBAC认证和集群之间的TLS通讯
			
elasticsearch基于RBAC认证和集群之间的TLS通讯 一.背景 二.需要解决的问题 三.给es增加用户名和密码访问 1.修改config/elasticsearch.yml 2.访问es集 ...
 - 计算机网络之传输层(传输层提供的服务及功能概述、端口、套接字--Socket、无连接UDP和面向连接TCP服务)
			
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105451022 学习课程:<2019王道考研计算机网络> 学习目的 ...
 - 单源最短路径算法:迪杰斯特拉 (Dijkstra) 算法(一)
			
一.算法介绍 迪杰斯特拉算法(英语:Dijkstra's algorithm)由荷兰计算机科学家艾兹赫尔·迪杰斯特拉在1956年提出.迪杰斯特拉算法使用了广度优先搜索解决赋权有向图的单源最短路径问题. ...
 - python读取、写入txt文本内容
			
转载:https://blog.csdn.net/qq_37828488/article/details/100024924 python常用的读取文件函数有三种read().readline().r ...
 - 嵌入式开发板nfs挂载
			
板子要开始调试了,第一个头大的问题就是调试过程中更新的文件怎么更新到板子上,以前用sd卡拷贝来来回回太浪费时间了,adb也需要接线各种连接操作. 现在板子有wifi可用,是时候把nfs共享搭起来了. ...
 - 使用google zxing生成二维码图片
			
生成二维码工具类: 1 import java.awt.geom.AffineTransform; 2 import java.awt.image.AffineTransformOp; 3 impor ...
 - K8S在线部署含Dashborad
			
参考文章 https://www.kubernetes.org.cn/5462.html 前言 Kubernetes作为容器编排工具,简化容器管理,提升工作效率而颇受青睐.很多新手部署Kubernet ...
 - AC-DCN ESXi
			
传统IT架构中的网络,根据业务需求部署上线以后,如果业务需求发生变动,重新修改相应网络设备(路由器.交换机.防火墙)上的配置是一件非常繁琐的事情.在互联网/移动互联网瞬息万变的业务环境下,网络的高稳定 ...
 - NSFOCUS建议您采取以下措施以降低威胁: * 修改源代码或者配置文件改变SSH服务的缺省banner。
			
老扫漏洞出来要整改 1.在 /etc/下创建一个文件 ssh_banner_change,在文件中输入内容,如:welcome! 2.修改/etc/ssh/sshd_config 文件,添加以下内容: ...
 - split,cdn,shell脚本,tmux,记一次往国外服务器传大文件的经历
			
需求是这样的:将一个大概680M的Matlab数据文件传到国外某所大学的服务器上,服务器需要连接VPN才能访问,由于数据文件太大,而且如果我直接ssh连过去或者用ftp传输,那么中间很可能中断. ps ...