class=”markdown_views prism-atom-one-dark”>
Algorithm idea:
To reverse the singly linked list, you should first think of using the head interpolation method, but it is required not to create a new node, you can use the elements in L as the insertion object, and set L->next to empty. Then the elements are inserted into L one by one using the head interpolation method.
void reverse(LNode* L) {
LNode* p = L->next;
LNode* q;
L->next = NULL;
while (p) {
q = p->next;//q is used to record the successor node of p
p->next = L->next;//The node pointed to by p is inserted into a new linked list
L->next = p;
p = q;
}
}