相关定义:
struct LNode{
int e;
LNode* next;
};
typedef struct LNode* LinkList;
非递归方法:
//l 是带头结点的单链表
void ReverseList(LinkList l){
if(l==NULL || l->next == NULL)
return;
LNode *p, *q, *r;
p = l->next;
q = p->next;
while( q != NULL){
r = q->next;
q->next = p;
p = q;
q = r;
}
l->next->next = NULL;
l->next = p;
}
递归方法:
LNode* ReverseList_Recursive(LNode* pNode,LinkList& l){
if ( (pNode == NULL) || (pNode->next == NULL) ){
l->next->next = NULL;
l->next = pNode;
return pNode;
}
LNode* temp = ReverseList_Recursive(pNode->next, l);
temp->next = pNode;
return pNode;
}
posted on 2009-06-03 22:47
iConnect 阅读(611)
评论(0) 编辑 收藏 所属分类:
数学&算法&数据结构