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