posts - 2,  comments - 0,  trackbacks - 0
相关定义:

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 阅读(601) 评论(0)  编辑  收藏 所属分类: 数学&算法&数据结构

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(2)

文章分类(17)

文章档案(16)

收藏夹(17)

搜索

  •  

最新评论

阅读排行榜

评论排行榜