Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for ListNode.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int val) {
- * this.val = val;
- * this.next = null;
- * }
- * }
- */
- public class Solution {
- public ListNode detectCycle(ListNode head) {
- if (head == null || head.next==null) {
- return null;
- }
- ListNode fast, slow;
- fast = head.next;
- slow = head;
- while (fast != slow) {
- if(fast==null || fast.next==null)
- return null;
- fast = fast.next.next;
- slow = slow.next;
- }
- while (head != slow.next) {
- head = head.next;
- slow = slow.next;
- }
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement