They say, given the head of a single linked list, return the inverted head.
Analysis: Inverting a singly linked list is a bit hard to do, and I’m pretty tired of it. Go straight to the code
node *NIZHI(node *h) { node *p,*ret,*mid,*q; p=h->next; mid=p->next; ret=h; for(; q;) { q=mid->next; mid->next=p; p=mid; mid=q; } ret->next->next=NULL; ret->next=p; return ret; }Copy the code
The core code is the one above, and what’s passed in is a single linked list of the leading nodes. Where p is the next node of the head node, mid is p->next, q records mid->next, and the loop ends when q is NUULL. Ret ->next->next is the pointer to the first node with an argument, which, when inverted, points to NULL. The header points to P, which is the first node with an argument after the inversion.