The reason for the code order in the reverse function to change the result is clear.
If you do not change the head address first, the address of mid-> link changes the address of head-> link, and the result is incorrect.
Head = head-> link; // this line
Mid-> link = last; // if this row exists, the result is incorrect. Why is it wrong?
# Include <stdio. h> <br/> # include <stdlib. h> <br/> typedef struct test <br/>{< br/> int num; <br/> struct test * link; <br/>} Node; <br/> Node * insert (Node * head, int num) <br/> {<br/> Node * new_node, * ptr = head; <br/> new_node = (Node *) malloc (sizeof (Node); <br/> if (! New_node) <br/>{< br/> printf ("error/n"); <br/> exit (1 ); <br/>}< br/> new_node-> num = num; <br/> new_node-> link = NULL; <br/> if (! Head) <br/>{< br/> return new_node; <br/>}< br/> while (ptr-> link! = NULL) <br/>{< br/> ptr = ptr-> link; <br/>}</p> <p> ptr-> link = new_node; <br/> return head; <br/>}< br/> Node * reverse (Node * head) <br/>{< br/> Node * last, * mid = NULL; <br/> while (head! = NULL) <br/>{< br/> last = mid; <br/> mid = head; // here the mid and head point to the same node <br/> head = head-> link; // update the head first, head and mid point to different nodes <br/> mid-> link = last; // the link of the mid is changed here. If the head is not updated before, the link of the head is changed. <br/>}< br/> return mid; <br/>}< br/> void show (Node * head) <br/> {<br/> Node * temp = head; <br/> while (temp! = NULL) <br/>{< br/> printf ("% d->", temp-> num); <br/> temp = temp-> link; <br/>}< br/> printf ("NULL/n"); <br/>}< br/> void frees (Node * head) <br/>{< br/> while (head! = NULL) <br/>{< br/> head = head-> link; <br/> free (head ); <br/>}< br/> int main () <br/>{< br/> Node * head = NULL; <br/> int n, m, I; <br/> printf ("How many numbers do I need to enter? N =: "); <br/> scanf (" % d ", & n); <br/> for (I = 1; I <= n; I ++) <br/>{< br/> printf ("Enter the % d Number:", I); <br/> scanf ("% d", & m ); <br/> head = insert (head, m); <br/>}< br/> head = reverse (head); <br/> printf ("result: "); <br/> show (head); <br/> frees (head); <br/> return 0; <br/>}