将不带头结点的链表进行逆置,一般可以采取用多个节点指针的方式。这里采用递归的方法。
1 node* reverse(node* head , node* front = NULL)//head为头指针,调用此函数时只需传入第一个参数。返回值是逆置后链表的头指针 2 { 3 if(head == NULL) 4 { 5 return head ; 6 } 7 if(head->next != NULL) 8 { 9 node* temp = reverse(head->next , head) ; 10 head->next = front ; 11 return temp ; 12 } 13 else 14 { 15 head->next = front ; 16 return head ; 17 } 18 }
本文介绍了一种使用递归方式实现链表逆置的算法。通过传递当前节点和前驱节点来完成逆置过程,最终返回逆置后的链表头指针。
755

被折叠的 条评论
为什么被折叠?



