chensheng@biheap.com:~$

List Detect Loop

// Java program to detect loop in a linked list 

int detectLoop() 
{ 
	Node slow_p = head, fast_p = head; 
	while (slow_p != null && fast_p != null && fast_p.next != null) { 
		slow_p = slow_p.next; 
		fast_p = fast_p.next.next; 
		if (slow_p == fast_p) { 
			System.out.println("Found loop"); 
			return 1; 
		} 
	} 
	return 0; 
}