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

  • Post author:
  • Post category:其他




方法一:三个指针

定义三个指针

pre(前驱指针)

初始化成NULL,

s初始化成NULL



p指向第一个数据的地址

(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;
	}



版权声明:本文为Tina14原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。