题解 | #判断链表中是否有环#
判断链表中是否有环
http://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
package sf;
public class hlb {
public static void main(String[] args) {
hhlb node1=new hhlb(1);
hhlb node2=new hhlb(2);
hhlb node3=new hhlb(3);
node1.next=node2;
node2.next=node3;
node3.next=node1;
boolean b0=hhb(node1);
System.out.println(b0);
} static boolean hhb(hhlb head){ hhlb l1=head; hhlb l2=head; if(head==null){ return false; } while (l2!=null&&l2.next!=null){ l1=l1.next; l2=l2.next.next; if (l1==l2){ return true; } } return false; }
}
class hhlb{
int val;
hhlb next;
public hhlb(int val) { this.val = val; }
}