1 #include <iostream> 2 using namespace std; 3 4 typedef int elem; 5 6 struct Node{ 7 elem n; 8 struct Node * next; 9 }; 10 11 //创建 12 Node* Create_link(){ 13 Node* root = new Node; 14 if(root==NULL) return NULL; 15 16 root->n = 1; 17 root->next = NULL; 18 19 Node* temp = root; 20 21 for(int i=2;i<=5;i++){ 22 Node* p = new Node; 23 p->n = i; 24 p->next = NULL; 25 26 temp->next = p; 27 temp = temp->next; 28 } 29 //delete temp; 30 return root; 31 } 32 33 //逆置 34 Node* Reversal(Node* head){ 35 Node *prev = NULL; 36 Node *next = NULL; 37 38 while(head){ 39 next = head->next; 40 head->next = prev; 41 prev = head; 42 head = next; 43 } 44 return prev; 45 } 46 47 //打印 48 void Print(Node* n){ 49 Node* temp = n; 50 while(temp!=NULL){ 51 cout<<temp->n<<' '; 52 temp = temp->next; 53 } 54 cout<<' '; 55 } 56 int main(){ 57 Node* L = Create_link(); 58 Print(L);//逆置前 59 Node* R = Reversal(L); 60 Print(R);//逆置后 61 return 0; 62 }
转自:https://blog.csdn.net/baidu_31945865/article/details/88361680