Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public ListNode detectCycle(ListNode head) {
- ListNode p1 = head;
- ListNode p2 = head;
- while(p2!=null&&p2.next!=null){
- p1=p1.next;
- p2=p2.next.next;
- if(p1==p2){
- break;
- }
- }
- if (p2 == null || p2.next == null){
- return null;
- }
- ListNode p3 = head;
- while(p3!= p2){
- p3=p3.next;
- p2=p2.next;
- }
- return p3;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement