LeetCode OJ-- Linked List Cycle II **
https://oj.leetcode.com/problems/linked-list-cycle-ii/
判断一个链表中是否有环,如果有,求环的开始位置。
按照上道题目的想法,先判断出是否有环来,同时能得出 slow走了多少步设为 paces。也就是说再次相遇的时候,fast比slow多走了环的n倍的大小。
然后slow = head, fast = head,然后让fast = fast->next往后走 paces 步,之后slow 和 fast再一起走,等到相遇的时候,就是那个环开始地方。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL)
return false; ListNode *slow = head;
ListNode *fast = head; int space = ;
while(fast)
{
if(fast->next)
fast = fast->next->next;
else
return NULL; slow = slow->next; space++; if(fast == slow)
break;
} if(fast == NULL)
return NULL;
slow = head;
fast = head;
while(space--)
{
fast = fast->next;
}
while(fast!=slow)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
int main()
{
class Solution mys;
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
/*ListNode *n3 = new ListNode(3);
ListNode *n4 = new ListNode(4);*/
n1->next = n2;
/*n2->next = n3;
n3->next = n4;
n4->next = n3;*/
mys.detectCycle(n1);
}
LeetCode OJ-- Linked List Cycle II **的更多相关文章
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 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 Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- leetcode 【 Linked List Cycle II 】 python 实现
公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...
- leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- Python_day01_作业笔记
内容大纲: 1. python的出生与应用以及历史, python2x: 源码冗余,源码重复,源码不规范. python3x: 源码清晰优美简单. 2. python的种类. Cpython: 官 ...
- python简单试题4
( ps : 题目中用到的一些random函数在最后末尾处有介绍) 1,在屏幕上显示跑马灯文字 import os # 调用os模块 import time # 调用时间模块 def main(): ...
- Jane Austen【简·奥斯汀】
Jane Austen Jane Austen, a famous English writer, was born at Steventon, Hampshire, on December 16, ...
- dfs 的全排列
#include <iostream> #include <algorithm> #include <cstdio> #include <string> ...
- 同步锁之lock
一. synchronized的缺陷 当一个代码块被synchronized修饰时,同时该代码块被一个线程执行,其他线程便只能一直等待,等待获取锁的线程释放锁,而这里获取锁的线程释放锁只会有两种情况: ...
- lambda表达式与函数接口的关系以及使用案例
lambda表达式与函数式接口是结合使用的. 函数式接口:接口中只有一个抽象方法的接口,其中可以包括default修饰,static 修饰的实例方法.函数式接口可以在接口上添加@FuncationIn ...
- 从零开始到设计Python+Selenium自动化测试框架-如何开始
如何开始学习web ui自动化测试?如何选择一门脚本语言?选择什么自动化测试工具? 本人已经做测试快5年,很惭愧,感觉积累不够,很多测试都不会,三年多功能测试,最近两年才开始接触和学习自动化测试.打算 ...
- python-day5-格式化输入
python格式化输入包含'%'调用,及format方法 使用‘%’进行格式化输出 #最简单的字符串传参 tpl='i am %s '%'alex' >>>i am alex #字符 ...
- 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:
nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1
- Emma中文乱码解决方法
vim -/.emma/emmarc db_encoding=latin1 改为 db_encoding=utf8 sudo vim /usr/share/emma/emmalib/mysql_hos ...