如何判断一个链表是否有环路
转自
http://www.iteye.com/topic/1114110
?
- public?static?boolean?hasLoop(Node?root)?{??
- ????r1?=?root.clone();??
- ???????????????r2?=?root.clone();??
- ???????????????while(true){??
- ???????????????????if(r1.hashnext){r1?=?r1.next();}else{break;}??
- ???????????????????if(r1.hashnext){r1?=?r1.next();}else{break;}??
- ??
- ???????????????????if(r2.hashnext){r2?=?r2.next();}else{break;}??
- ??
- ???????????????????if(r1?==?r2?)?return?true;??
- ??
- ???????????????}??
- ??
- ??
- ????return?false;??
- } ?