There are many ways to reverse the linked list. One is to move the pointer to the last element every time, and take this element down, put it in the header of A New linked list, and end with the reverse, scanning the chain table n-1 times in total. Obviously, this is relatively slow. Another method is to change the pointer direction from the Start Node to the end, and scan the linked list from the start to the end, which is much more efficient than the previous one. The code for this method is as follows:
# Include
# Include
Typedef struct Info {int data; struct Info * next;} Node, * pNode; int main () {void create (pNode); void reverse (pNode ); void print (pNode); pNode link; link = (pNode) malloc (sizeof (Node); create (link); reverse (link); print (link ); return 0;} void create (pNode head) {int n = 0; pNode p, q; p = q = (pNode) malloc (sizeof (Node )); scanf ("% d", & p-> data); while (p-> data! = 0) {++ n; if (n = 1) head-> next = p; else q-> next = p; q = p; p = (pNode) malloc (sizeof (Node); scanf ("% d", & p-> data);} q-> next = NULL;} void reverse (pNode head) {pNode p0, p1, p2; if (head-> next = NULL & head-> next = NULL) /* If this linked list does not have one or only one element, you do not need to perform reverse operations */return; p0 = NULL; p2 = head-> next; /* p2 points to the first element */p1 = head-> next;/* p1 points to the second element */while (p1! = NULL) {p2-> next = p0;/* First, disconnects the link between the elements that p1 points to and the elements that p2 points, let p2 point to the element's pointer to its previous element */p0 = p2;/* the following three steps have completed the pointer backward shift and prepare the next cycle */p2 = p1; p1 = p1-> next;} p2-> next = p0;/* The Last Time p1 is empty, the loop is exited, so we need to execute another operation to let p2 point to the element's pointer to its previous element */head-> next = p2; /* change the header node to */} void print (pNode head) {pNode p = head-> next; while (p! = NULL) {printf ("% d", p-> data); p = p-> next;} putchar (10 );}