目录
有头节点
三指针法
void Reverse(Node* head) { Node* p,* q, *r=NULL; p = head->next; q = p->next; p->next = NULL; while (q) { r = q; q = q->next; r->next = p; p = r; } head->next = r; }
递归法
void Reverse(Node* head,Node* p)//p=p->next { if (p == NULL || p->next == NULL)//分别对应p为空表,p不为空表 { head->next = p; } else { Node * pnext=p->next; Reverse(head,pnext); pnext->next = p; p->next = NULL; } }
头插法
void Reverse_head(Node*head) { Node* p, * q; p = head->next; head->next = NULL; while (p) { q = p->next; p->next = head->next; head->next = p; p = q; } }
无头节点
三指针法
Node* Reverse(Node* head) { Node* p, * q, * r=NULL; p = head; q = p->next; head->next = NULL; while (q) { r = q; q = q->next; r->next = p; p = r; } return r; }
头插法
Node* Reversebyhead(Node*head) { Node* p, * q; p = head->next; head->next = NULL; while (p) { q = p->next; p->next = head; head = p; p = q; } return head; }