0
点赞
收藏
分享

微信扫一扫

C++-list使用学习

萨摩斯加士奇 2024-10-01 阅读 12
算法

 单链表反转(逆置)——(四种方法实现)_单链表的逆置-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;
	}
举报

相关推荐

0 条评论