【Leetcode】【Medium】Linked List Cycle
Given a linked list, determine if it has a cycle in it.
解题:
判断单链表是否具有环,使用两个指针once和twice遍历链表,once一次走一步,twice一次走两步,如果相遇,则说明有环,否则没有。
原因是,如果单链表具有环,不论once和twice进入环的位置如何,由于twice每次比once多走一步,类似操场跑步,twice最终会追上once。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if (head == NULL || head->next == NULL)
return false;
ListNode* once = head->next;
ListNode* twice = head->next->next; while (once && twice && twice->next) {
if (once == twice)
return true;
once = once->next;
twice = twice->next->next;
} return false;
}
};
【Leetcode】【Medium】Linked List Cycle的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 【LeetCode每天一题】Reverse Linked List(链表反转)
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...
- 【leetcode刷题笔记】Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 【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刷题笔记】Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
随机推荐
- SQL基础语法select|insert|update|delete(增删改查) 简单使用
以下案列以此表举例 1.select(查询) select简单的查询分为两种 注:字段也就是表结构中的列的名称 第一种: select 字段名 from 表名 此种查询只列出你所需要查询的字段, ...
- CSAPP阅读笔记-struct, union, 数据对齐-来自第三章3.9的笔记-P183-P191
1.数据对齐 为什么要对齐:通俗点解释就是CPU对数据访问时,每次都是取固定数量的字节数,假如一次取4个字节,若有个int存在0x01-0x04,则一次就能取出,若存在0x03-0x06,则需要分两次 ...
- 与native交互时会出现的问题
1.jsbridge: 可以用jsbridge与native交互,这属于第三方库,前端后端都需要加jsbridge 2.可以直接调用原生的方法,ios: window.webkit.message ...
- (转)Apache和Nginx运行原理解析
Apache和Nginx运行原理解析 原文:https://www.server110.com/nginx/201402/6543.html Web服务器 Web服务器也称为WWW(WORLD WID ...
- Oracle TM锁和TX锁
CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...
- 9、在Shell脚本中调用其他脚本
在Shell脚本的执行过程中,Shell脚本支持调用另一个Shell脚本,调用的格式为:程序名 实例:在Shell脚本test1中调用test2. 1.调用test2#test1脚本root@ubun ...
- 攻克数据库核心技术壁垒,实现百万级QPS的高吞吐
CynosDB是腾讯云自研的新一代高性能高可用的企业级分布式云数据库.融合了传统数据库.云计算与新硬件的优势,100%兼容开源数据库,百万级QPS的高吞吐,不限存储,价格仅为商用数据库的1/10. C ...
- Linux学习(1)
Linux操作系统核心"Kernel",位于操作系统底层,是连接Shell.KDE.应用和硬件的接口,核心必须支持的管理事物: 1)系统调用接口(System Call Inter ...
- vue-cli 基础搭建
1.安装node 2.npm install webpack -g 3.npm install vue-cli -g 4.然后进入到文件下边 vue init webpack 文件名字 5.进入工程文 ...
- win10中VirtualBox联网设置
<分享>关于win10操作系统中VirtualBox无法桥接的解决方法 版权声明:本文为博主原创文章,未经博主允许不得转载. 升级win10,本来是一件很好的事,想好好体验一下新版本的感觉 ...