看图理解单链表的反转

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;
ActList* p;
ActList* q;
ActList* r;
while(q){
r = q->next; //先保留下一个step要处理的指针
q->next = p; //然后p q交替工作进行反向
p = q;
q = r;
}
}

updated 2014-01-24，重新非IDE环境写了一遍

struct ListNode{
int val;
ListNode* next;
ListNode(int a):val(a),next(NULL){}
};
ListNode* m=NULL; //充当搬运工作用的节点
while(r!=NULL){  //bug2 循环语句写错了, while写成了if
m=r;
r=r->next;
m->next=p->next;
p->next=m;
//if(r!=NULL)
//std::cout<<"m="<<m->val<<" ,p="<<p->val<<" ,r="<<r->val<<std::endl;
//else
//std::cout<<"m="<<m->val<<" ,p="<<p->val<<" ,r=NULL"<<std::endl;
}
tail->next=p;
p->next=NULL;
tail=p;
}

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

p->next->next=NULL;//断掉环
}


//创建:用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*  Create()
{//start of CREATE()
ActList* p=NULL;
ActList* q=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;

{
}
else
{
q->next=p;
}
q=p;
cout<<"Please input the length of the movie."<<endl;
cin>>Time;
}
q->next=NULL;

}//end of CREATE()

{//start of display
cout<<"show the list of programs."<<endl;
{
}
}//end of display

{
//ActList* temp=new ActList;
ActList* p;
ActList* q;
ActList* r;
while(q){
r = q->next; //
q->next = p;
p = q; //
q = r; //
}
}

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

p->next->next=NULL;//断掉环
}
int main(int argc, char* argv[])
{
//	DisplayList(Create());
//  DisplayList(ReverseList2(Create()));
DisplayList(ReverseList3(Create()));
return 0;
}


updated: 2014-01-24

// 递归版的第一种实现，借助类的成员变量m_phead来表示新链表的头指针。
struct ListNode{
int val;
ListNode* next;
ListNode(int a):val(a),next(NULL){}
};

class Solution{
return NULL;
}
}
};



struct ListNode{
int val;
ListNode* next;
ListNode(int a):val(a),next(NULL){}
};

class Solution{
return NULL;
}