141 . Linked List Cycle
Easy
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
1ms:
public boolean hasCycle(ListNode head) {
if(head==null) return false;
ListNode slow = head;
ListNode fast = head;
while(fast.next!=null&&fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow==fast)
return true;
}
return false;
}
142 . Linked List Cycle II
Medium
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.
Follow up:
Can you solve it without using extra space?