/**
* Source : https://oj.leetcode.com/problems/linked-list-cycle/
*
* Given a linked list, determine if it has a cycle in it.
*
* Follow up:
* Can you solve it without using extra space?
*/
public class LinkedListCycle { /**
* 判断一个链表是否有循环
*
* 遍历链表,将所有遍历过的ndeo放入hash表,如果当前节点已经在hash表中,说明有循环,直到遍历结束。
*
* 上线的方法需要额外的空间,使用双指针法:
* slow指针每次移动一个node,fast每次移动两个node,在遍历结束前,如果slow == fast,那么该链表是循环的
*
*
* @param head
* @return
*/
public boolean hasCycle (LinkedNode head) {
if (head == null) {
return false;
}
LinkedNode slow = head;
LinkedNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
} private class LinkedNode {
int value;
LinkedNode next; } /**
* 创建普通的链表
* @param arr
* @return
*/
public LinkedNode createList (int[] arr) {
if (arr.length == 0) {
return null;
}
LinkedNode head = new LinkedNode();
head.value = arr[0];
LinkedNode pointer = head;
for (int i = 1; i < arr.length; i++) {
LinkedNode node = new LinkedNode();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
} /**
* 将链表变为循环链表,循环起始为第index个node
* @param head
* @param index
*/
public void makeCycle (LinkedNode head, int index) {
if (head == null) {
return;
}
LinkedNode tail = head;
int count = 1;
while (tail.next != null) {
tail = tail.next;
count++;
}
LinkedNode p = head;
if (index > count) {
index = index % count;
} else if (index < 0) {
index = Math.abs(index);
}
while (p != null) {
index--;
if (index < 1) {
tail.next = p;
break;
}
p = p.next;
} } public static void main(String[] args) {
LinkedListCycle linkedListCycle = new LinkedListCycle();
LinkedNode list = linkedListCycle.createList(new int[]{1,2,3,4,5}); System.out.println(linkedListCycle.hasCycle(list) + " == false"); linkedListCycle.makeCycle(list, 2);
System.out.println(linkedListCycle.hasCycle(list) + " == true");
}
}

leetcode — linked-list-cycle的更多相关文章

  1. 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 ...

  2. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  4. [算法][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 ...

  5. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. LeetCode: Linked List Cycle 解题报告

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  8. LeetCode Linked List Cycle 解答程序

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

  9. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  10. [LeetCode] Linked List Cycle II, Solution

    Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...

随机推荐

  1. thinkphp5.0 微信公众号接入支付宝支付

    ---恢复内容开始--- 真是无力吐槽这个需求了,想骂客户,好端端的非要在微信公众号接入支付宝,都知道微信公众号是拒绝支付宝的,屏蔽了支付宝,所以在微信公众号接入支付宝的话就必须手动复制链接跳出微信内 ...

  2. 【深度学习系列】用PaddlePaddle和Tensorflow实现经典CNN网络Vgg

    上周我们讲了经典CNN网络AlexNet对图像分类的效果,2014年,在AlexNet出来的两年后,牛津大学提出了Vgg网络,并在ILSVRC 2014中的classification项目的比赛中取得 ...

  3. python中csv文件的读取问题

    在python读取csv格式的文件时,使用csv.reader读取文件对象,出现了line contains NULL byte的错误,如下: reader = csv.reader(open(fil ...

  4. .NET Core快速入门教程 2、我的第一个.NET Core App(Windows篇)

    一.前言 本篇开发环境?1.操作系统: Windows 10 X642.SDK: .NET Core 2.0 Preview 二.安装 .NET Core SDK 1.下载 .NET Core下载地址 ...

  5. 【luogu P1613】跑路

    https://www.luogu.org/problem/show?pid=1613 看到2k就能想到倍增.用一个数组avai[i][j][k]表示点i与点j是否存在长2k的路径,则可以递推出ava ...

  6. 页面输入的数据格式转换类:BaseAction(经常使用于Struts框架中)

    在我们接收页面传来的数据时,这些数据都是以String类型接收的,所以要进行数据格式转换,这时候就能够统一为它们进行转换,并且在处理这些数据的类中能够继承ActionSupport类,然后让每个接收数 ...

  7. 理解vuex的状态管理模式架构

    理解vuex的状态管理模式架构 一: 什么是vuex?官方解释如下:vuex是一个专为vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证以一种可预测的 ...

  8. Mongodb 3.4 + Centos6.5 配置 + mysql.sql转为csv 或 json导入和导出Mongo (64位系统)

    Centos下通过yum安装步骤如下: 声明:相对比那些用源码安装,少了配置和新建log和data目录,这种简单粗暴, ,创建仓库文件, vi /etc/yum.repos.d/mongodb-org ...

  9. 前端MVC Vue2学习总结(一)——MVC与vue2概要、模板、数据绑定与综合示例

    一.前端MVC概要 1.1.库与框架的区别 框架是一个软件的半成品,在全局范围内给了大的约束.库是工具,在单点上给我们提供功能.框架是依赖库的.Vue是框架而jQuery则是库. 1.2.AMD与CM ...

  10. Md5加密秘钥加密哈希加密

    加密通用类: public class EncryptClass { /// <summary> /// 返回MD5加密字符串 /// </summary> /// <p ...