求助高手:怎么样把一个链表倒置呢?

hualuchi 2009-06-05 10:52:11
求助高手:怎么样把一个链表倒置呢?
...全文
187 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheng_fengming 2010-10-15
  • 打赏
  • 举报
回复
原来写的单链表逆序的例子

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stru)

struct stru
{
int num;
struct stru *next;
};
typedef struct stru pNode;

int n;
pNode *create_list();
void print_list(pNode *head);
pNode *reverse_list(pNode *head);

void main()
{
pNode *head = NULL, *newhead = NULL;
printf("Creating the list,input the num(end of string):\n");
head = create_list();
printf("The original list is:\n");
print_list(head);
newhead = reverse_list(head);
printf("\nThe new list is:\n");
print_list(newhead);
}

pNode *reverse_list(pNode *head)
{
int i;
pNode *p1, *p2, *newhead, *newp;
for (i = 0; i <= n; i++)
{
p2 = p1 = head;
while (p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (i == 0)
{
newhead = newp = p1;
}
else
{
newp = newp->next = p1;
p2->next = NULL;
}
}
return (newhead);
}

其他的函数定义没有贴上来,省地方,呵呵
某某9 2010-10-15
  • 打赏
  • 举报
回复

Node* LinkList_reverse(Node* head)

{

Node *preNode,*curNode,*nextNode;

if(head==NULL) return NULL;//空链表

if(head->next == NULL) return head;//仅一个元素

curNode = head;preNode=NULL;//初始化

while(curNode)

{

nextNode = curNode->next;//先记录下一个结点

curNode->next = preNode;//改变链表方向(逆置)

preNode = curNode;//将当前结点作为下一次循环的前一个结点

curNode = nextNode;//向后推移一个结点

}

return preNode;//当遍历完链表后curNode应该为空,此时preNode就是逆置后链表头(head)
}
alicktony 2010-10-15
  • 打赏
  • 举报
回复
其实最简洁应该是递归
foolloow 2010-10-15
  • 打赏
  • 举报
回复
笔试时考到了,当时写的比较乱,这个很清楚。
十八道胡同 2009-06-06
  • 打赏
  • 举报
回复
node*  ReverseList(node*  head)  
{
node* p=0;
node* q=head; // now q is the head of new list
while(head->next!=0) // head node is always the same, but its next node will change
{
p=head->next; // get the next node, it is the current node
head->next=p->next; // set the head->next to the next node, using in next loop
p->next=q; // make the current node pointer the head of new list
q=p; // set the q as the head of new list
}
return q; // now q is the head of new list
}

cxf7394373 2009-06-06
  • 打赏
  • 举报
回复
void Reverse(List & L)
{
List p=L->next; // 原链表
L->next=NULL; // 新表(空表)
while ( p )
{
// 从原链表中取下结点s
List s=p;
p=p->next;
// 插入L新表表头
s->next=L->next;
L->next=s;
}
}
  • 打赏
  • 举报
回复
反向建立一新链表不就得了.....
hualuchi 2009-06-06
  • 打赏
  • 举报
回复
谢谢…………
luxiaoxun 2009-06-05
  • 打赏
  • 举报
回复
void Reverse(List & L)
{
List p=L->next; // 原链表
L->next=NULL; // 新表(空表)
while ( p )
{
// 从原链表中取下结点s
List s=p;
p=p->next;
// 插入L新表表头
s->next=L->next;
L->next=s;
}
}
adventurelw 2009-06-05
  • 打赏
  • 举报
回复
用反向迭代器。
wuyu637 2009-06-05
  • 打赏
  • 举报
回复
以前csdn上的代码,重贴一下
#include <iostream>
using namespace std;

class node
{
public:
node(int x):data(x),next(0){}
int data;
node* next;
};

node* ReverseList(node* head)
{
node* p=0;
node* q=head; // now q is the head of new list
while(head->next!=0) // head node is always the same, but its next node will change
{
p=head->next; // get the next node, it is the current node
head->next=p->next; // set the head->next to the next node, using in next loop
p->next=q; // make the current node pointer the head of new list
q=p; // set the q as the head of new list
}
return q; // now q is the head of new list
}

int main()
{
node* head=new node(0);
node* tail=head;
for(int i=1;i<10;i++)
{
node* p=new node(i);
tail->next=p;
tail=p;
}
head=ReverseList(head);
while (head)
{
cout<<head->data<<endl;
head=head->next;
}
getchar();
return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧