带头结点单链表逆置的四种方法思路和实现

方法一:三个指针

 定义三个指针pre(前驱指针)初始化成NULL,s初始化成NULLp指向第一个数据的地址

(1)pre=NULL,s=NULL,p//链表不为空或链表不止一个节点

(2)s=p;//s下移

 (3)p=p->next; //p下移          

 

  (5)  s->next=pre;//s的下一位为自己(s)的上一位,即ai指向ai+1

 

(4)pre=s;//pre指向s,pre下移

 

  实现代码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"My_LinkList.h"
void ReverseList(LinkList head)//空链表不用逆置
{
	assert(head!=NULL);
	if (head->next == NULL || head->next->next == NULL)//单链表为NULL或只有一个节点,不用逆置
	{
		return;//退出
	}
	ListNode * pre = NULL, * s = NULL;
	ListNode* p = head->next;
	while (p != NULL)
	{
		s = p;
		p = p->next;
		s->next = pre;//ai+1指向ai
		pre = s;
	}
	head->next = pre;
}
int main()
{
	LinkList head = InitList;
	for (int i = 0; i < 10; i++)
	{
		Push_Back(head, i + 1);
	}
	PrintList(head);
	ReverseList(head);
	return 0;
}

方法二:两个指针(头插法)

(1)s=NULL,p;//p不为NULL,循环开始

(2)p!=NULL;

(3)s=p;//p,s同时指向首节点

 

(4)p=p->next;//p后移

 

(5)s->next=head->next;//头插,s指向首节点的next域,被置为空

 

(6)head->next=s;//s中存放的是含有23节点的首地址,head->next存放的也是23节点的首地址

 循环继续

(2)p!=NULL;(2)p!=NULL;4)p=p->next;

 

(5)s->next=head->next;

 

 (6)head->next=s;

 最后效果;

 实现代码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"My_LinkList.h"
void ReverseList(LinkList head)//空链表不用逆置
{
	assert(head!=NULL);
	if (head->next == NULL || head->next->next == NULL)//单链表为NULL或只有一个节点,不用逆置
	{
		return;//退出
	}
	ListNode* s = NULL, * p = head->next;
	head->next = NULL;
	while (p != NULL)
	{
		s = p;
		p = p->next;
		s->next = head->next;
		head->next = s;
	}
}
int main()
{
	LinkList head = InitList;
	for (int i = 0; i < 10; i++)
	{
		Push_Back(head, i + 1);
	}
	PrintList(head);
	ReverseList(head);
	return 0;
}

方法三:栈(用栈的先进后出,单链表数据全部入栈之后,然后把带头结点的数据结点给p,数据出栈,然后覆盖原数据)

栈逆置方法1

图示:

数据入栈

数据出栈

 

 

实现代码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"My_LinkList.h"
	void ReverseList(LinkList head)//空链表不用逆置
	{
		assert(head != NULL);
		if (head->next == NULL || head->next->next == NULL)//单链表为NULL或只有一个节点,不用逆置
		{
			return;//退出
		}
		int* stack = (int*)malloc(sizeof(int) * len);
		int top = -1;//为模拟栈做准备
		p = head->next;
		while (p != NULL)//入栈
		{
			top += 1;
			stack[top] = p->data;
			p = p->next;
		}
		p = p->head->next;
		while (p != NULL)//出栈
		{
			p->data = stack[top];
			top = top - 1;
			p = p->next;
		}
		free(stack);
		stack = NULL:
	}

	int main()
	{
		LinkList head = InitList;
		for (int i = 0; i < 10; i++)
		{
			Push_Back(head, i + 1);
		}
		PrintList(head);
		ReverseList(head);
		return 0;
	}

栈逆置方法2:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"My_LinkList.h"
	void ReverseList(LinkList head)//空链表不用逆置
	{
		assert(head != NULL);
		if (head->next == NULL || head->next->next == NULL)//单链表为NULL或只有一个节点,不用逆置
		{
			return;//退出
		}
		int len = 0;
		ListNode* p = head->next;
		while (p != NULL)//O(n)
		{
			len++;
			p = p->next;
		}
		int* stack = (int*)malloc(sizeof(int) * len);
		int top = -1;
		p = p->next;
	}
	p = head->next;
	while (p != NULL)//O(n)
	{
		p->data = stack[top];
		top = top - 1;
		p = p->next;
	}
	free(stack);
	stack = NULL;
	}

	int main()
	{
		LinkList head = InitList;
		for (int i = 0; i < 10; i++)
		{
			Push_Back(head, i + 1);
		}
		PrintList(head);
		ReverseList(head);
		return 0;
	}

方法四:递归

p->next->next=p;

p->next=NULL:

实现代码:

#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->next == NULL || head->next->next == NULL)//单链表为NULL或只有一个节点,不用逆置
		{
			return;//退出
		}
		ListNode* p = head->next;
		head->next = 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;
	}

  • 35
    点赞
  • 286
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值