自学内容网 自学内容网

不带头结点单链表逆置递归实现---未验证

 单链表反转(逆置)——(四种方法实现)_单链表的逆置-CSDN博客

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"My_LinkList.h"
ListNode* Reverse(ListNode* p)
{
if (p == NULL || p->next == NULL)
{
return p;
}
ListNode* firstnode = Reverse(p->next);
p->next - next = p;
p->next = NULL;
return firstnode;
}
void ReverseList(LinkList head)//空链表不用逆置
{
assert(head != NULL);
if (head== NULL || head->next== NULL)//单链表为NULL或只有一个节点,不用逆置
{
return;//退出
}
//ListNode* p = head->next;
//head->next = Reverse(p);

        ListNode* p = head;
        head = Reverse(p);//不带头节点单链表逆置---写一下运行一下
}
int main()
{
LinkList head = InitList;
for (int i = 0; i < 10; i++)
{
Push_Back(head, i + 1);
}
PrintList(head);
ReverseList(head);
return 0;
}


原文地址:https://blog.csdn.net/weixin_62349327/article/details/142654807

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!