Small-Chao

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2011年5月14日

摘要: 一、如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针) struct node { char val; node* next;}bool check(const node* head) {} //returnfalse : 无环;true: 有环 一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):bool check(const node* head) { if(head==NULL) return false; node *low=head, *fast=head->next; while(fast!=N 阅读全文
posted @ 2011-05-14 21:49 Small-Chao 阅读(211) 评论(0) 推荐(0) 编辑