怎么判断一个链表是否有环路

如何判断一个链表是否有环路转自http://www.iteye.com/topic/1114110?public?static?boolean?hasLoop(Node

如何判断一个链表是否有环路

转自

http://www.iteye.com/topic/1114110

?

  1. public?static?boolean?hasLoop(Node?root)?{??
  2. ????r1?=?root.clone();??
  3. ???????????????r2?=?root.clone();??
  4. ???????????????while(true){??
  5. ???????????????????if(r1.hashnext){r1?=?r1.next();}else{break;}??
  6. ???????????????????if(r1.hashnext){r1?=?r1.next();}else{break;}??
  7. ??
  8. ???????????????????if(r2.hashnext){r2?=?r2.next();}else{break;}??
  9. ??
  10. ???????????????????if(r1?==?r2?)?return?true;??
  11. ??
  12. ???????????????}??
  13. ??
  14. ??
  15. ????return?false;??
  16. } ?