最常见的程序员面试题(1)单链表反向

来源:互联网 发布:炉石传说盒子数据在哪 编辑:程序博客网 时间:2024/06/13 03:56
一个单链表如何反向? 在不增加额外链表对象的情况下。仔细的分析这个题目可以知道: 一个N长度的链表反向需要N次循环,也就是每次循环要改变一个next指针的值。同时,为了让循环能继续,当前的cur->next指针要保存下来,作为下次运行的cur指针。在用一个指针来保存反向后,新的next指针要指向的位置。

    分别用C++和Java来实现。

[cpp] view plaincopyprint?
  1. #include "stdafx.h"   
  2. struct node{  
  3.     int data;  
  4.     node* pNext;  
  5.     explicit node(int i)  
  6.         :data(i),pNext(nullptr){}  
  7.     ~node(){if(pNext)delete pNext;}  
  8. };  
  9. struct sl{  
  10.     node* pHead;  
  11.     node* pTail;  
  12.     sl():pHead(nullptr),pTail(nullptr){}  
  13.     ~sl(){if(pHead)delete pHead;}  
  14.     void append(node* ps){  
  15.         if(nullptr==pHead){  
  16.             pHead=ps;  
  17.             pTail=ps;  
  18.         }  
  19.         else{  
  20.             pTail->pNext=ps;  
  21.             pTail=ps;  
  22.         }  
  23.     }  
  24.     void display(){  
  25.         node* pCur=pHead;  
  26.         while(pCur){  
  27.             printf("%d\n",pCur->data);  
  28.             pCur=pCur->pNext;  
  29.         }  
  30.     }  
  31.     void revert(){  
  32.         node* pCur=pHead;  
  33.         if(pCur==pTail)return;  
  34.         pTail=pHead;  
  35.         node* pNext=nullptr;  
  36.         while(pCur){  
  37.             node* pPrev = pCur->pNext;  
  38.             pCur->pNext = pNext;  
  39.             if(pPrev==nullptr)break;  
  40.             pNext=pCur;  
  41.             pCur =pPrev;  
  42.         }  
  43.         pHead = pCur;  
  44.     }  
  45. };  
  46. int _tmain(int argc, _TCHAR* argv[])  
  47. {  
  48.     sl list1;  
  49.     list1.append(new node(2));  
  50.     list1.append(new node(6));  
  51.     list1.append(new node(8));  
  52.     list1.append(new node(7));  
  53.     list1.append(new node(5));  
  54.     list1.display();  
  55.     printf("reverted\n");  
  56.     list1.revert();  
  57.     list1.display();  
  58.     return 0;  
  59. }  


 

[java] view plaincopyprint?
  1. public class RevertList {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         RevertList.list li=new RevertList.list();  
  9.         li.append(2);  
  10.         li.append(3);  
  11.         li.append(5);  
  12.         li.append(6);  
  13.         li.append(10);  
  14.         li.display();  
  15.         System.out.println("================");  
  16.         li.revert();  
  17.         li.display();  
  18.     }  
  19.     static class node{  
  20.         int data;  
  21.         node next;  
  22.         node(int d){  
  23.             data=d;  
  24.             next=null;  
  25.         }  
  26.     }  
  27.     static class list{  
  28.         node pHead;  
  29.         node pTail;  
  30.         list(){  
  31.             pHead=pTail=null;  
  32.         }  
  33.         void append(int n){  
  34.             if(null==pHead){  
  35.                 pHead=new node(n);  
  36.                 pTail=pHead;  
  37.             }  
  38.             else{  
  39.                 pTail.next=new node(n);  
  40.                 pTail=pTail.next;  
  41.             }  
  42.         }  
  43.         void display(){  
  44.             node Cur=pHead;  
  45.             while(Cur!=null){//?  
  46.                 System.out.println( "=" + Cur.data );  
  47.                 Cur=Cur.next;  
  48.             }  
  49.         }  
  50.         void revert(){  
  51.             node Cur=pHead;  
  52.             if(Cur==pTail){  
  53.                 return;  
  54.             }  
  55.             pTail=pHead;  
  56.             node Revert=null;  
  57.             while(Cur!=null){  
  58.                 node Next=Cur.next;  
  59.                 Cur.next=Revert;  
  60.                 if(Next==null)break;  
  61.                 System.out.println("inside revert");  
  62.                 Revert=Cur;  
  63.                 Cur=Next;  
  64.             }  
  65.             pHead=Cur;  
  66.         }  
  67.     }  
  68. }