LeetCode142. Linked List Cycle II
来源:互联网 发布:java脱壳工具 编辑:程序博客网 时间:2024/05/17 08:17
142.Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }}
方法一:利用 HashSet
利用 HashSet ,HashSet 中出现的第一个重复的元素就是链表中的环的头。
public ListNode detectCycle(ListNode head) { Set<ListNode> nodeset = new HashSet<>(); while(head != null){ if(nodeset.contains(head)){ return head; } else{ nodeset.add(head); head = head.next; } } return head; }
方法二:两个指针
这里设置了两个指针,一个速度快的指针,一个速度慢的指针。
设环的长度为 N ,进入环之前的距离为 A ,相遇时慢指针走过的距离为 B。
速度快的指针的速度是速度慢的指针的 2 倍,所以两个指针第一次相遇时,快的指针
正好比慢的指针多走了一倍的距离,也就是多走了一圈,即 A + B = N.
所以在第一个相遇之后,慢指针再移动 A 的距离就能到环的起点。
想象成下图比较好理解一些:
public ListNode detectCycle2(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast!=null && fast.next!=null){ fast = fast.next.next; slow = slow.next; if (fast == slow){ ListNode slow2 = head; while (slow2 != slow){ slow = slow.next; slow2 = slow2.next; } return slow; } } return null;}
阅读全文
0 0
- LeetCode142:Linked List Cycle II
- LeetCode142 Linked List Cycle II
- leetcode142. Linked List Cycle II
- Leetcode142. Linked List Cycle II
- leetcode142. Linked List Cycle II
- leetcode142~Linked List Cycle II
- [leetcode142]Linked List Cycle II
- leetcode142.Linked List Cycle II
- LeetCode142. Linked List Cycle II
- LCP142 LeetCode142. Linked List Cycle II
- LeetCode142—Linked List Cycle II
- Leetcode142 - Linked List Cycle II(链表)
- c++实现LeetCode142. Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Spring boot + shiro 跨域配置(解决jsessionid丢失问题)
- c#特性简述使用
- JS同时验证固话和手机号正则表达式
- buildroot重新编译package
- Linux学习(Make和Makefile)
- LeetCode142. Linked List Cycle II
- 心得
- SQLAlchemy使用笔记一
- Android基础——Tween动画、Drawable动画、Property动画、MaterialDesign动画、Trasition动画
- Spring 事物的配置以及容易出现的错误
- 对线性回归、逻辑回归、各种回归的概念学习
- Kotlin 学习笔记 整理
- JAVA gc垃圾回收机制
- innodb的索引的概述