[Leetcode] Linked List Cycle II
来源:互联网 发布:centos ssh key登录 编辑:程序博客网 时间:2024/05/16 08:40
Reference: http://yucoding.blogspot.com/2013/12/leetcode-question-linked-list-cycle-ii.html
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.
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detectCycle(ListNode head) { ListNode slowNode = head; ListNode fastNode = head; while(fastNode != null) { fastNode = nextNodeWithStrikeTwo(fastNode); slowNode = nextNode(slowNode); if(fastNode == null) { return null; } if (fastNode == slowNode){ return findStartPoint(head, fastNode); } } return null; } private ListNode nextNode(ListNode node) { return node.next; } private ListNode nextNodeWithStrikeTwo(ListNode node) { if (node.next == null) { return null; } return node.next.next; } private ListNode findStartPoint(ListNode head, ListNode meetingNode) { while(head != meetingNode) { head = nextNode(head); meetingNode = nextNode(meetingNode); } return head; }}
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Lambda表达式详细总结
- 2-13 无名管道通信
- 地图相关
- c++四大类型转换函数
- 关于for-each与删除集合节点的思考
- [Leetcode] Linked List Cycle II
- JAVA笔试题笔记(三)
- CSS实现单行、多行文本溢出显示省略号(…)
- 对于面向对象里加强
- LeetCode283Move Zeroes
- 微信公众平台jsapi开发教程(3)页面初步接入微信jsapi
- Redis学习笔记
- MJExtension使用指导(转)
- 第一本十三章人机猜拳