Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Node firstNode(Node head) {
- Node slow = head, fast = head;
- // 1->2->3->4
- while(fast.next != null && fast.next.next != null){
- slow = slow.next;
- fast = fast.next.next;
- if(slow == fast){
- break;
- }
- }
- if(fast.next == null || fast.next.next == null){
- return null;
- }
- Node slow2 = head;
- while(slow.next != null){
- if(slow == slow2) return slow;
- slow = slow.next;
- slow2 = slow2.next;
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement