【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 ...
随机推荐
- 《数据密集型应用系统设计》读书笔记-ch1可靠、可扩展与可维护的应用系统
我们以Twitter为例,使用其2012年11月发布的数据.Twitter的两个典型业务操作是: - 发布tweet消息: 用户可以快速推送新消息到所有的关注者,平均大约4.6k request/se ...
- MySQL约束和修改数据表知识集结
一.约束 划分标准:功能.数据列的数目 功能: (1)NOT NULL(非空约束) (2)PRIMARY KEY(主键约束) (3)UNIQUE(唯一约束) (4)DEFAULT(默认约束) (5)F ...
- CMS: DNN And Umbraco
在比较了众多CMS系统后,还是把焦点定在DNN和Umbraco两个系统上,这两个系统都可以使用UserControl扩展自己需要的功能. DNN的架构比较复杂,Module.Skin.Containe ...
- CSS禁止滚动条
CSS禁止滚动条的方法: 1.完全隐藏 在<boby>里加入scroll="no",可隐藏滚动条: <boby scroll="no"> ...
- Express4.10.2开发框架中默认app.js的代码注释
//通过require()加载了express.path等模块var express = require('express');var path = require('path');var favic ...
- WPF的RadioButton--单选框
1. 使用, 显示的内容改为Content属性 <RadioButton Content="boy"/> 2. 要使用分组,就是用 GroupName属性 <Ra ...
- Android OpenGL教程-第五课【转】
第五课 3D空间: 我们使用多边形和四边形创建3D物体,在这一课里,我们把三角形变为立体的金子塔形状,把四边形变为立方体. 先看看三角形的顶点变成啥了 private float[] mTriangl ...
- resteasy上传单个文件/多个文件到阿里云服务器
代码如下: ExcelServerController.java package com.xgt.controller; import com.xgt.bean.bs.ExcelBean; impor ...
- Yii框架配置语言包
配置文件frontend\config\main.php 'language' => 'zh-CN', //配置语言包 'i18n' =>[ 'translations' => [ ...
- 用c+libcurl+PCRE写爬虫1--编译libcurl
打算用c语言和libcurl库在windows下实现一些爬虫操作. 一.编译libcurl 1.编译zlib 1)下载zlib http://sourceforge.net/projects/libp ...