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的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- Nginx负载均衡使用ip
upstream test1{ server 192.168.1.213; server 192.168.1.37; } server { listen 80; # default backlog=2 ...
- BP算法从原理到python实现
BP算法从原理到实践 反向传播算法Backpropagation的python实现 觉得有用的话,欢迎一起讨论相互学习~Follow Me 博主接触深度学习已经一段时间,近期在与别人进行讨论时,发现自 ...
- H5混合开发二维码扫描以及调用本地摄像头
今天主管给了我个需求,说要用混合开发,用H5调用本地摄像头进行扫描二维码,我之前有做过原生安卓的二维码扫一扫,主要是通过调用zxing插件进行操作的,其中还弄了个闪光灯.但是纯H5的没接触过,心里没底 ...
- select联动遇到的问题
今天写了个select下拉联动,记录下过程中遇到的问题. dom部分 // 拿到选中项的索引 myselect.selectedIndex // 拿到选中项的text/value myselect.o ...
- Java----前端验证之验证码额实现
验证码是常用的登录验证方式之一,最大的作用就是保证安全,验证码的生成在java中实现的方式有很多种,比如后台生成传输到前端页面,在前台直接生成进行验证,下面写一个最简单实现验证码验证登录的例子. 生成 ...
- Sencha Cmd 6 和 Ext JS 6 指南文档(部分官方文档中文翻译)
近期组织了几个程序员网友,正在翻译一部分官方的Sencha Cmd 6 和 Ext JS 6 指南文档. 眼下还没翻译完,大家能够先看看 Sencha Cmd 6 和 Ext JS 6 指南文档 ( ...
- Object-C中release的机制问题
今天写了例如以下的一段代码: for (NSInteger i = 0; i < 10000000; i++) { UIView * v = [[UIView alloc] init]; [v ...
- 剑指 offer代码解析——面试题39推断平衡二叉树
题目:输入一颗二叉树的根结点.推断该树是不是平衡二叉树. 假设某二叉树中随意结点的左右子树的高度相差不超过1,那么它就是一棵平衡二叉树. 分析:所谓平衡二叉树就是要确保每一个结点的左子树与右子树的高度 ...
- TCP传输中序号与确认序号的交互
本实验通过SSH远程登录server,然后使用Wireshark抓包分析. 开头的三次握手已经省略.关于序号的交互过程.须要记住一点:TCP首部中的确认序号表示已成功收到字节,但还不包括确认序号所指的 ...
- jQuery->JavaScript一览表
比较jQuery与JavaScript的不同功能实现 CSS选择器 元素选择 // jQuery $("li").css("color", "red& ...