Topic:
Given A linked list, determine if it has a cycle in it.
Ideas:
For judging whether the chain list has a ring, the method is very simple, with two pointers, beginning to point to the head node, one is a fast pointer, two steps at a time, a slow pointer, one step at a time, when two pointers coincide with the existence of the ring.
Fast first into the ring, after the slow entered, if the slow as in front, fast in the back of each loop to slow near 1, so will meet, and will not appear fast directly skip slow situation.
/** Definition for singly-linked list. * Function ListNode (val) {* This.val = val; * This.next = null; *}
*//** * @param {listnode} head * @return {Boolean}*/varHascycle =function(head) {if(head==NULL|| head.next==NULL){ return false; } vars=head,f=Head.next.next; while(s!=f) { if(f==NULL|| f.next==NULL){ return false; }Else{s=S.next; F=F.next.next; } } return true;};
"Linked list" Linked list Cycle