1. /**
  2. * Source : https://oj.leetcode.com/problems/linked-list-cycle-ii/
  3. *
  4. * Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
  5. *
  6. * Follow up:
  7. * Can you solve it without using extra space?
  8. */
  9. public class LinkedListCycle2 {
  10. /**
  11. * 如果链表是循环的,则找到循环的开始节点
  12. * 否则,返回null
  13. *
  14. * 依然使用双指针法,如果是循环链表的话,
  15. * slow和fast第一次相遇之后,将slow = head,
  16. * 然后将slow和fast每次移动一个节点,第二次相遇的时候就是循环的起始节点
  17. *
  18. * 原理:
  19. * 假设head到start的距离a,slow和fast第一次相遇的位置距离start为b,循环链表周长为c,第一次相遇的时候fast走过的长度一定是slow的两倍,在过程中遍历了循环链表n次
  20. * (a + b) * 2 = a + n * c + b
  21. * 那么a = n*c - b => a + b = n*c
  22. * 也就是说,现在让两个指针slow指向head,fast指向第一次相遇的位置,然后每次两个指针移动一个节点,直到下一次相遇,正好走过n个周长,即下一次相遇在start位置
  23. *
  24. * @param head
  25. * @return
  26. */
  27. public LinkedNode findCycleStart (LinkedNode head) {
  28. if (head == null) {
  29. return null;
  30. }
  31. LinkedNode slow = head;
  32. LinkedNode fast = head;
  33. while (fast.next != null && fast.next.next != null) {
  34. slow = slow.next;
  35. fast = fast.next.next;
  36. if (slow == fast) {
  37. break;
  38. }
  39. }
  40. if (fast == slow) {
  41. // 链表存在循环
  42. slow = head;
  43. while (slow != fast) {
  44. slow = slow.next;
  45. fast = fast.next;
  46. }
  47. return slow;
  48. }
  49. return null;
  50. }
  51. private class LinkedNode {
  52. int value;
  53. LinkedNode next;
  54. }
  55. /**
  56. * 创建普通的链表
  57. * @param arr
  58. * @return
  59. */
  60. public LinkedNode createList (int[] arr) {
  61. if (arr.length == 0) {
  62. return null;
  63. }
  64. LinkedNode head = new LinkedNode();
  65. head.value = arr[0];
  66. LinkedNode pointer = head;
  67. for (int i = 1; i < arr.length; i++) {
  68. LinkedNode node = new LinkedNode();
  69. node.value = arr[i];
  70. pointer.next = node;
  71. pointer = pointer.next;
  72. }
  73. return head;
  74. }
  75. /**
  76. * 将链表变为循环链表,循环起始为第index个node
  77. * @param head
  78. * @param index
  79. */
  80. public void makeCycle (LinkedNode head, int index) {
  81. if (head == null) {
  82. return;
  83. }
  84. LinkedNode tail = head;
  85. int count = 1;
  86. while (tail.next != null) {
  87. tail = tail.next;
  88. count++;
  89. }
  90. LinkedNode p = head;
  91. if (index > count) {
  92. index = index % count;
  93. } else if (index < 0) {
  94. index = Math.abs(index);
  95. }
  96. while (p != null) {
  97. index--;
  98. if (index < 1) {
  99. tail.next = p;
  100. break;
  101. }
  102. p = p.next;
  103. }
  104. }
  105. public static void main(String[] args) {
  106. LinkedListCycle2 linkedListCycle = new LinkedListCycle2();
  107. LinkedNode list = linkedListCycle.createList(new int[]{1,2,3,4,5});
  108. System.out.println(linkedListCycle.findCycleStart(list) + " == null");
  109. linkedListCycle.makeCycle(list, 2);
  110. System.out.println(linkedListCycle.findCycleStart(list).value + " == 2");
  111. }
  112. }

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

  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 解题报告

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

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

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

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

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

  5. [LeetCode] Linked List Cycle II, Solution

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

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. LeetCode——Linked List Cycle II

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

  8. Leetcode Linked List Cycle II

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

  9. [LeetCode] Linked List Cycle II 链表环起始位置

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

  10. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. springboot添加swagger2组件

    swagger2是一个可以构建和调试RESTful API文档的组件,利用swagger2的注解可以快速的在项目中构建Api文档,并且提供了测试API的功能 1,引入依赖 <dependency ...

  2. vue.js初学,笔记1,安装

    最近学习vue.js,下面是笔记: 说明:因为npm安装插件是从国外服务器下载,受网络影响大,可能出现异常,如果npm的服务器在中国就好了,所以我们乐于分享的淘宝团队干了这事.来自官网:"这 ...

  3. 智能合约语言 Solidity 教程系列4 - 数据存储位置分析

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 这部分的内容官方英文文档讲的不是很透,因此我在参考Soli ...

  4. 二分PkU3258

    <span style="color:#330099;">/* E - 二分 Time Limit:2000MS Memory Limit:65536KB 64bit ...

  5. cocos2d+TexturePackerGUI动画制作

    转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25168047 程序效果图: 1.下载安装TexturePackerGUI 地址:htt ...

  6. ElasticSearch和ElasticSearch Head环境搭建和数据模拟

    首先elasticsearch-6.0.0\bin目录下运行elasticsearch服务 修改elasticsearch-6.0.0\elasticsearch.yml文件 在文件最后加入下面代码, ...

  7. css怎样让背景充满整个屏幕

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 腾讯云实力通过工信部测评,获全国范围CDN经营许可

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 近日,腾讯云提前完成了全国范围的CDN资质测评,顺利获得工信部颁发的CDN业务全国范围的经营许可证. 2017年1月份,工信部发布<关于 ...

  9. ASP.NET Core 使用 Hangfire 定时任务

    定时任务组件,除了 Hangfire 外,还有一个 Quarz.NET,不过 Hangfire .NET Core 支持的会更好些. ASP.NET Core 使用 Hangfire 很简单,首先,N ...

  10. Material使用08 MdDialogModule、MdAutocompleteModule

    1 MatDialog 1.1 简要描述 MdDialog是一个服务,可以利用它来打开一个具有material风格和动画效果的对话框 技巧01:虽然已经在模块级别导入了MdDialogModule但是 ...