将不带头结点的链表进行逆置,一般可以采取用多个节点指针的方式。这里采用递归的方法。

 

 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 }

 

相关文章:

  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2021-11-06
  • 2021-10-14
  • 2021-09-24
  • 2021-10-25
  • 2021-09-08
猜你喜欢
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-08-24
  • 2021-06-06
相关资源
相似解决方案