【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 ...
随机推荐
- 浅解bootstrap 下拉框插件
最近的业务需求是把后台数据渲染到前端,其中有一个是复选框数组,前端选择渲染的方式是bootstrap的下拉插件 渲染完成之后,此时会在原有select同级上面生成buton,dropdown-menu ...
- c# 短链接生成
public static string GetShortUrl(string url) { //可以自定义生成MD5加密字符传前的混合KEY string key = DateTime.Now.To ...
- Apache-jmeter3.3安装
一.首先检查机子上是否有安装jdk 检查方式,在cmd中输入java,出现如下信息,即已经安装好jdk 若未安装jdk,则看如下步骤 步骤一: 1.下载jdk,到官网下载jdk,地址:http://w ...
- C语言实现链表
#include<stdio.h>#include<malloc.h>#include<stdlib.h> typedef struct Node { int ...
- 1.4 js基础
1.eval(字符串),将字符串变成可执行的语句. 太强大了,太危险了 .目前不要去使用. 注入攻击 2.作用域[起作用的范围] 全局变量:声明在函数外 ...
- Golang教程:变量
声明单一变量 声明一个变量的语法为:var name type,例如 package main import "fmt" func main() { var age int // ...
- java 配置在.properties文件中的常量
不让用常量类,那就用.properties文件配置,放在根目录. import java.util.HashMap; import java.util.Iterator; import java.ut ...
- linux修改用户名和密码
linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...
- javaEE Design Patter(2)详解3个设计模式
Factory (bean creating) Proxy(Agent)(Aop)书有~ Templete(springMVC Facelets)//此处及以上忽略 工厂模式(Factory) 工厂模 ...
- MongoDB 学习(三)MongoDB 和 Spring 整合(Maven)
一.MongoDB 和 Spring 整合(Maven) 1.相关 jar 包准备 2.用 Maven 创建项目,pom.xml 文件 <project xmlns="http://m ...