leetcode — linked-list-cycle-ii
/**
* Source : https://oj.leetcode.com/problems/linked-list-cycle-ii/
*
* Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
*
* Follow up:
* Can you solve it without using extra space?
*/
public class LinkedListCycle2 {
/**
* 如果链表是循环的,则找到循环的开始节点
* 否则,返回null
*
* 依然使用双指针法,如果是循环链表的话,
* slow和fast第一次相遇之后,将slow = head,
* 然后将slow和fast每次移动一个节点,第二次相遇的时候就是循环的起始节点
*
* 原理:
* 假设head到start的距离a,slow和fast第一次相遇的位置距离start为b,循环链表周长为c,第一次相遇的时候fast走过的长度一定是slow的两倍,在过程中遍历了循环链表n次
* (a + b) * 2 = a + n * c + b
* 那么a = n*c - b => a + b = n*c
* 也就是说,现在让两个指针slow指向head,fast指向第一次相遇的位置,然后每次两个指针移动一个节点,直到下一次相遇,正好走过n个周长,即下一次相遇在start位置
*
* @param head
* @return
*/
public LinkedNode findCycleStart (LinkedNode head) {
if (head == null) {
return null;
}
LinkedNode slow = head;
LinkedNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
if (fast == slow) {
// 链表存在循环
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
return null;
}
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) {
LinkedListCycle2 linkedListCycle = new LinkedListCycle2();
LinkedNode list = linkedListCycle.createList(new int[]{1,2,3,4,5});
System.out.println(linkedListCycle.findCycleStart(list) + " == null");
linkedListCycle.makeCycle(list, 2);
System.out.println(linkedListCycle.findCycleStart(list).value + " == 2");
}
}
leetcode — linked-list-cycle-ii的更多相关文章
- 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 ...
- 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 ...
- [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] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- 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 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- php IP转换整形(ip2long)
如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...
- mysql数据库插入数据获取自增主键的三种方式(jdbc PreparedStatement方式、mybatis useGeneratedKeys方式、mybatis selectKey方式)
通常来说对于mysql数据库插入数据获取主键的方法是采用selectKey的方式,特别是当你持久层使用mybatis框架的时候. 本文除此之外介绍其它两种获取主键的方式. 为了方便描述我们先建一张my ...
- PHP 使用Echarts生成数据统计报表
echarts统计,心血来潮~~ 先看下效果图 看下代码 HTML页面 为ECharts准备一个Dom,宽高自定义 <div class="panel panel-info" ...
- js----数组处理之splice(有js原始addClass方法哦)
上次写了一个轮播的方法:http://blog.csdn.net/stronglyh/article/details/46833499 由于别人问我的时候,给了我html.于是乎我就看到了页面中引用了 ...
- 页面的新开页,window.open的hacker
一.window.open如何进行hack 网上看的办法很多,归根接地还是不能解决掉,只有通过a标签的target属性 $obj.click(function(){ var newTab=windo ...
- spring+hibernate项目demo搭建
之前用maven+spring+mybatis+spring mvc搭建了一个web项目,用于学习spring及相关知识,现在打算将mybatis换成hibernate,一样搭建一个框架. 其实myb ...
- ssm学习(四)--完整的增删改查demo
上一篇文章简要介绍了将sping mvc加入整个框架,算是完成了ssm的集成.本节继续前面的内容,结合spring mvc做一个简单的增删改查demo. 1.首先,重写一下GeckoList.jsp页 ...
- 【转载】Java系列笔记(3) - Java 内存区域和GC机制
Java系列笔记(3) - Java 内存区域和GC机制 转载:原文地址http://www.cnblogs.com/zhguang/p/3257367.html 目录 Java垃圾回收概况 Java ...
- C#应用程序隐藏调用bat脚本
做c#应用程序有些调用windows自带的bat脚本会比较方便 Process proc; proc = null; try { string targetDir = GetParentUrl() + ...
- Linux:如何进行c++编程
不适应美帝的饮食,当一只咸鱼在apartment里Coding一波,学习学习如何在Ubuntu实现C++的编程 正文如下: (预备知识) 学习Vim: http://www.cnblogs.com/ ...