1. 程式人生 > >再說單鏈表反轉

再說單鏈表反轉

AD status ini light save link class ret cpp

struct Node{
   Value data;
   Node* next;              
}

Node* ReverseSingleLinkedList(Node* head){
   //inital status
   Node* p = head;
   Node* q = head;   
   head = NULL;  // initialize new head as NULL

   while(p != NULL){
      
      q = p->next;  //save p‘s next
      p->next = head;    
      head = p;// set new head
      p=q;  //to proceed
   };   

   return head;
}

  

再說單鏈表反轉