摘要: 思想为:设置两个指针,一个步长为1,另一个步长为2,依次后移,如果相遇且都不为空,则有环。与这个类似的问题包括:怎么快速检测出一个巨大的链表中的死链?或者如何找出一个单链表的中间节点?代码为:bool loop(node* head){bool flag = true;if (head == NULL){ flag = false;}node* one = head;node* two = head->next;if (two == NULL){ flag = false;}while (one != two){ if (one != NULL) { one = one->next 阅读全文
posted @ 2012-02-03 09:32 zp_Alex 阅读(554) 评论(0) 推荐(0) 编辑
摘要: 思想为:head指针不断后移,指针反向即可,代码为:#include <stdio.h>#include <conio.h>#include <malloc.h>#include <stdlib.h>typedef struct node{int data;struct node *next;}node,*LinkList;void creat(LinkList &L){node *p,*q;L=(LinkList)malloc(sizeof(node));if (L==NULL){exit(0);}L->next=NULL;L-& 阅读全文
posted @ 2012-02-03 09:29 zp_Alex 阅读(308) 评论(0) 推荐(0) 编辑