• 单链表的反转


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

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


     

    第三次循环。。。。。

     

     

    具体代码如下

     

     

    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个节点挪到新表的表尾即可。

    代码如下:

     

     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<<" "<<head->ActName<<" "<<head->Director<<" "<<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;   

    int main(int argc, char* argv[]) 

    //  DisplayList(Create()); 
    //  DisplayList(ReverseList2(Create())); 
        DisplayList(ReverseList3(Create())); 
        return 0; 

     

    转自:http://www.2cto.com/kf/201110/106607.html

  • 相关阅读:
    java spring boot- freemarker 配置 yml使用流程
    layer 漂亮的弹窗
    react-native 打包apk 更新js和常见问题
    mysql 运行中 偶尔 报错 2002 也许是这个问题,内存不足导致的
    关于rsa公钥格式的处理,一行纯内容进行换行格式化
    第十篇、让UIScrollView的滚动条常显
    第九篇、自定义底部UITabBar
    第八篇、封装NSURLSession网络请求框架
    第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
    第七篇、OC_图片的裁剪基于SDWebImage
  • 原文地址:https://www.cnblogs.com/NINIiOS/p/4826787.html
Copyright © 2020-2023  润新知