看图理解单链表的反转

如何把一个单链表进行反转?

方法1:将单链表储存为数组,然后按照数组的索引逆序进行反转。

方法2:使用三个指针遍历单链表,逐个链接点进行反转。

方法3:从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,最后将第一个节点挪到新表的表尾。


 

方法1:

浪费空间。


 

方法2:

使用p和q连个指针配合工作,使得两个节点间的指向反向,同时用r记录剩下的链表。


p = head;

q = head->next;


head->next = NULL;


现在进入循环体,这是第一次循环。

r = q->next;

q->next = p;


p = q;

q =r;


第二次循环。

r = q->next


q->next = p;    


p = q;


q = r


第三次循环。。。。。



具体代码如下

[cpp]  view plain copy
  1. ActList* ReverseList2(ActList* head)  
  2. {  
  3.     //ActList* temp=new ActList;  
  4.  if(NULL==head|| NULL==head->next) return head;      
  5.     ActList* p;  
  6.     ActList* q;  
  7.     ActList* r;  
  8.     p = head;    
  9.     q = head->next;  
  10.     head->next = NULL;  
  11.     while(q){  
  12.         r = q->next; //  
  13.         q->next = p;      
  14.         p = q; //  
  15.         q = r; //  
  16.     }  
  17.     head=p;  
  18.     return head;      
  19. }  


方法3

还是先看图,


从图上观察,方法是:对于一条链表,从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,(N-1)次这样的操作结束之后将第1个节点挪到新表的表尾即可。

代码如下:

[cpp]  view plain copy
  1. ActList* ReverseList3(ActList* head)  
  2. {  
  3.     ActList* p;  
  4.     ActList* q;  
  5.     p=head->next;  
  6.     while(p->next!=NULL){  
  7.         q=p->next;  
  8.         p->next=q->next;  
  9.         q->next=head->next;  
  10.         head->next=q;  
  11.     }  
  12.   
  13.     p->next=head;//相当于成环  
  14.     head=p->next->next;//新head变为原head的next  
  15.     p->next->next=NULL;//断掉环  
  16.     return head;    
  17. }  


附:

完整的链表创建,显示,反转代码:

[cpp]  view plain copy
  1. //创建:用q指向当前链表的最后一个节点;用p指向即将插入的新节点。  
  2. //反向:用p和q反转工,r记录链表中剩下的还未反转的部分。  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8. struct ActList  
  9. {  
  10.     char ActName[20];  
  11.     char Director[20];  
  12.     int Mtime;  
  13.     ActList *next;  
  14. };  
  15.   
  16. ActList* head;  
  17.   
  18. ActList*  Create()  
  19. {//start of CREATE()  
  20.     ActList* p=NULL;  
  21.     ActList* q=NULL;  
  22.     head=NULL;  
  23.     int Time;  
  24.     cout<<"Please input the length of the movie."<<endl;  
  25.     cin>>Time;  
  26.     while(Time!=0){  
  27.     p=new ActList;  
  28.     //类似表达:  TreeNode* node = new TreeNode;//Noice that [new] should be written out.  
  29.     p->Mtime=Time;  
  30.     cout<<"Please input the name of the movie."<<endl;  
  31.     cin>>p->ActName;  
  32.     cout<<"Please input the Director of the movie."<<endl;  
  33.     cin>>p->Director;  
  34.   
  35.     if(head==NULL)  
  36.     {  
  37.     head=p;  
  38.     }  
  39.     else  
  40.     {  
  41.     q->next=p;  
  42.     }  
  43.     q=p;  
  44.     cout<<"Please input the length of the movie."<<endl;  
  45.     cin>>Time;  
  46.     }  
  47.     if(head!=NULL)  
  48.     q->next=NULL;  
  49.     return head;  
  50.   
  51. }//end of CREATE()  
  52.   
  53.   
  54. void DisplayList(ActList* head)  
  55. {//start of display  
  56.     cout<<"show the list of programs."<<endl;  
  57.     while(head!=NULL)  
  58.     {  
  59.         cout<<head->Mtime<<"\t"<<head->ActName<<"\t"<<head->Director<<"\t"<<endl;  
  60.         head=head->next;  
  61.     }  
  62. }//end of display  
  63.   
  64.   
  65. ActList* ReverseList2(ActList* head)  
  66. {  
  67.     //ActList* temp=new ActList;  
  68.  if(NULL==head|| NULL==head->next) return head;      
  69.     ActList* p;  
  70.     ActList* q;  
  71.     ActList* r;  
  72.     p = head;    
  73.     q = head->next;  
  74.     head->next = NULL;  
  75.     while(q){  
  76.         r = q->next; //  
  77.         q->next = p;      
  78.         p = q; //  
  79.         q = r; //  
  80.     }  
  81.     head=p;  
  82.     return head;      
  83. }  
  84.   
  85. ActList* ReverseList3(ActList* head)  
  86. {  
  87.     ActList* p;  
  88.     ActList* q;  
  89.     p=head->next;  
  90.     while(p->next!=NULL){  
  91.         q=p->next;  
  92.         p->next=q->next;  
  93.         q->next=head->next;  
  94.         head->next=q;  
  95.     }  
  96.   
  97.     p->next=head;//相当于成环  
  98.     head=p->next->next;//新head变为原head的next  
  99.     p->next->next=NULL;//断掉环  
  100.     return head;    
  101. }  
  102. int main(int argc, char* argv[])  
  103. {  
  104. //  DisplayList(Create());  
  105. //  DisplayList(ReverseList2(Create()));  
  106.     DisplayList(ReverseList3(Create()));  
  107.     return 0;  
  108. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值