The turn of the linked list is often tested, and there are many methods.
I saw one of the most intuitive and easy-to-understand ones on the Internet today, and excerpted them for your reference.
Reprinted from: http://blog.csdn.net/feliciafay/article/details/6841115
Method: Use 3 pointers to traverse the singly linked list, and reverse the link points one by one.
Steps: Use the two pointers p and q to work together to make the points between the two nodes reverse, and use r to record the remaining linked list.
p = head;
q = head->next;
head->next = NULL;
Now enter the body of the loop, which is the first loop.
r = q->next;
q->next = p;
p = q
q =r;
Second cycle.
r = q->next
q->next = p;
p = q
q = r
The third cycle. . . . .
code show as below:
ActList* ReverseList2(ActList* head) { //ActList* temp=new ActList; if(NULL==head|| NULL==head->next) return head; //There is no need to reverse if there are less than two nodes. ActList* p; ActList* q; ActList* r; p = head; q = head->next; head->next = NULL; //The old head pointer is the new tail pointer, next needs to point to NULL while(q){ r = q->next; //First keep the pointer to be processed by the next step q->next = p; //Then pq works alternately for reverse p = q q = r; } head=p; // Finally q must point to NULL, so p is returned as the new head pointer return head; }