看图理解单链表的反转

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

方法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


第三次循环。。。。。



具体代码如下

ActList* ReverseList2(ActList* head)
{
	//ActList* temp=new ActList;
 if(NULL==head|| NULL==head->next) return head;    
    ActList* p;
	ActList* q;
	ActList* r;
    p = head;  
    q = head->next;
    head->next = NULL;
    while(q){
        r = q->next; //
        q->next = p;    
        p = q; //
        q = r; //
    }
	head=p;
    return head;    
}

package com.sailang;

public class Node1
{
	int data;
	Node1 next;
	
	public Node1(int data)
	{
		this.data = data;
	}
}


package com.sailang;

public class ReverseLink
{
	public static void main(String[] args)
	{
		Node1 node1 = new Node1(1);
		Node1 node2 = new Node1(2);
		Node1 node3 = new Node1(3);
		Node1 node4 = new Node1(4);
		
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		node4.next = null;
		
		Node1 p = null;
		p = node1;	
		
		System.out.println("原始单链表为:");
		while(null != p)
		{
			System.out.println(p.data);
			p = p.next;
		}
		
		System.out.println("反转单链表为:");
		p = reverseLink(node1);
		while(null != p)
		{
			System.out.println(p.data);
			p = p.next;
		}
	}
	
	//反转链表函数
	public static Node1 reverseLink(Node1 head)
	{
		if(null == head || null == head.next)
			return head;
		
		Node1 p = head;
		Node1 q = head.next;
		head.next = null;
		Node1 r = null;//用于指向剩余部分
		while(null != q)
		{
			r = q.next;
			q.next = p;
			p = q;
			q = r;
		}
		head = p;
		return head;
	}
}



方法3

还是先看图,


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

代码如下:

ActList* ReverseList3(ActList* head)
{
	ActList* p;
	ActList* q;
	p=head->next;
	while(p->next!=NULL){
		q=p->next;
		p->next=q->next;
		q->next=head->next;
		head->next=q;
	}

	p->next=head;//相当于成环
	head=p->next->next;//新head变为原head的next
	p->next->next=NULL;//断掉环
	return head;  
}


附:

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

  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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值