I was in the previous blog, "C language to achieve a single list (not the lead node) in reverse print" in detail to achieve a not the first node of the list of the reverse order printing, the whole idea is also very simple, that is, sequentially traversing the original linked list, and then the removed node with the head interpolation method to establish a new linked list, the new linked list is the original list of reverse order. This blog I will be implemented using the lead node of the linked list to achieve reverse order, the idea is the same as above. Code uploaded to Https://github.com/chenyufeng1991/ReverseLinkedList_HeadNode.
The core code is as follows:
/**
* The whole idea is to take out a node, and then use the head interpolation method to create a new list, the new linked list is in reverse order.
*/
Node *reverselist (node *pnode) {
node *reverselist;
Initiallist (&reverselist);//The linked table Node *pmove is initialized in reverse order
;
Node *pinsert;
Pinsert = (node*) malloc (sizeof (Node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pmove = pnode->next;
while (Pmove!= NULL) {
//header interpolation
pinsert->element = pmove->element;
Pinsert->next = reverselist->next;
Reverselist->next = Pinsert;
Insert node reallocation memory
Pinsert = (node*) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pmove = pmove->next;
}
printf ("%s function execution, reverse order lead node's single linked list creation succeeded \ n", __function__);
Printlist (reverselist);
return reverselist;
}