单链表的反转
来源:互联网 发布:linux系统时间戳 编辑:程序博客网 时间:2024/05/21 01:42
方法1:带头结点
使用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;
- }
不带头结点
- #include<stdio.h>
- #include<stdlib.h>
- struct Node{
- int data;
- Node * next;
- };
- /*
- 3 -> 1 -> 4 -> 6 -> 2 (1)
- 1 -> 3 -> 4 -> 6 -> 2 (2)
- 4 -> 1 -> 3 -> 6 -> 2 (3)
- 6 -> 4 -> 1 -> 3 -> 2 (4)
- 2 -> 6 -> 4 -> 1 -> 3 (5)
- 循环反转,即依次改动3个指针值,直到链表反转完成
- 比如,上面第(1)行到第(2)行,过程如下
- head(原本指向3) 指向 1;
- 3(原本指向1) 指向 4;
- 1(原本指向4) 指向 3;
- */
- void Rev_link(Node * & head){
- Node * _cur,* _next,*tmp;
- _cur = head;
- _next = _cur->next;
- tmp = _cur;
- while(_cur->next != NULL){
- head = _next;
- _cur->next = _next->next;
- _next->next = tmp;
- _next = _cur->next;
- tmp = head;
- }
- }
方法2
还是先看图,
从图上观察,方法是:对于一条链表,从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,(N-1)次这样的操作结束之后将第1个节点挪到新表的表尾即可。
代码如下:
view plainActList* 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;
}
附:
完整的链表创建,显示,反转代码:
view plain//创建:用q指向当前链表的最后一个节点;用p指向即将插入的新节点。
//反向:用p和q反转工,r记录链表中剩下的还未反转的部分。
#include "stdafx.h"
#include <iostream>
using namespace std;
struct ActList
{
char ActName[20];
char Director[20];
int Mtime;
ActList *next;
};
ActList* head;
ActList* Create()
{//start of CREATE()
ActList* p=NULL;
ActList* q=NULL;
head=NULL;
int Time;
cout<<"Please input the length of the movie."<<endl;
cin>>Time;
while(Time!=0){
p=new ActList;
//类似表达: TreeNode* node = new TreeNode;//Noice that [new] should be written out.
p->Mtime=Time;
cout<<"Please input the name of the movie."<<endl;
cin>>p->ActName;
cout<<"Please input the Director of the movie."<<endl;
cin>>p->Director;
if(head==NULL)
{
head=p;
}
else
{
q->next=p;
}
q=p;
cout<<"Please input the length of the movie."<<endl;
cin>>Time;
}
if(head!=NULL)
q->next=NULL;
return head;
}//end of CREATE()
void DisplayList(ActList* head)
{//start of display
cout<<"show the list of programs."<<endl;
while(head!=NULL)
{
cout<<head->Mtime<<"\t"<<head->ActName<<"\t"<<head->Director<<"\t"<<endl;
head=head->next;
}
}//end of display
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;
}
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;
}
方法3::(递归)
- Node * Tmd(Node * head,Node *pre){//头和前驱
- Node * p = head->next; //保存头的下一个元素,用来递归
- head->next = pre; //链表的反向,即 next 指针指向 其前驱
- if(p!=NULL)
- return Tmd(p,head);
- else
- return head;
- }
- 单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表的反转 c++
- 单链表的反转
- 单链表的反转
- 单链表的反转问题
- 单链表的反转实现
- 2、单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表的反转
- 单链表反转的实现
- 单链表的反转
- 单链表的反转
- java date类型转换为json
- android软件开发之webView.addJavascriptInterface循环渐进
- /usr/bin/ld: cannot find -lpng
- UIImage相关
- 上传图片本地预览
- 单链表的反转
- 排序之直接插入排序
- 记录php5.4与php-redis冲突的解决
- VB 中chr(10)、chr(13)和vblf、vbcr、vbcrlf的分别
- div 垂直居中
- UITableView相关
- easyui tree 一些常见操作
- C语言实现大数整数乘法
- Linux下Nagios的安装与配置