C++反转链表(四种思路:数组记录、递归实现、循环实现、头删头插)

思路一:使用数组保存各个结点的指针,然后使用循环,实现各个指针的反向

这只是我自己想的一种方法,可能有些蠢,但是确实能够实现链表的反转。

#include<iostream>

using namespace std;

typedef struct node
{
	int data;
	struct node* next;
}*linklist,node;

int main() {
	//创建链表,元素为5个
	//这里主要是为了简单,没有使用头插法和尾插法,而是直接构造一个链表
	linklist head = new node;
	linklist n1p = new node;
	linklist n2p = new node;
	linklist n3p = new node;
	linklist n4p = new node;
	linklist n5p = new node;
	n1p->data = 1;
	n2p->data = 2;
	n3p->data = 3;
	n4p->data = 4;
	n5p->data = 5;
	head->next = n1p;
	n1p->next = n2p;
	n2p->next = n3p;
	n3p->next = n4p;
	n4p->next = n5p;
	n5p->next = NULL;
	
	if (head->next != NULL) {//保证链表非空
		//输出链表内容
		linklist p = head;
		int n = 0;
		while (p->next != NULL) {
			cout << p->next->data << endl;
			p = p->next;
			n++;
		}
		cout << endl;

		//把链表内容记录到数组中
		linklist* narr = new linklist[n];
		int i = 0;
		p = head;
		while (p->next != NULL) {
			narr[i++] = p->next;
			p = p->next;
		}

		//把数组中的内容输出
		for (i = 0; i < n; i++) {
			cout << narr[i]->data << endl;
		}
		cout << endl;

		//反转链表
		for (i = 0; i < n - 1; i++) {
			narr[i + 1]->next = narr[i];
		}
		narr[0]->next = NULL;
		head->next = narr[n - 1];

		//输出反转之后链表的内容
		p = head;
		while (p->next != NULL) {
			cout << p->next->data << endl;
			p = p->next;
		}
		cout << endl;
	}
	return 0;
}
思路二:使用递归实现

以自己对递归粗浅的认识写的,写的也很蠢,仅仅做记录

#include<iostream>

using namespace std;

typedef struct node
{
	int data;
	struct node* next;
}*linklist,node;

//反转链表的递归实现
void reverseLinkList(linklist& head,linklist& another) {
	//another是一个空链表的表头
	if (head->next == NULL) 
		return;
	reverseLinkList(head->next, another);
	linklist p = another;
	while (p->next != NULL) {
		p = p->next;
	}
	p->next = head->next;
	p->next->next = NULL;
}


int main() {
	//创建链表,元素为5个
	//这里主要是为了简单,没有使用头插法和尾插法,而是直接构造一个链表
	linklist head = new node;
	linklist n1p = new node;
	linklist n2p = new node;
	linklist n3p = new node;
	linklist n4p = new node;
	linklist n5p = new node;
	n1p->data = 1;
	n2p->data = 2;
	n3p->data = 3;
	n4p->data = 4;
	n5p->data = 5;
	head->next = n1p;
	n1p->next = n2p;
	n2p->next = n3p;
	n3p->next = n4p;
	n4p->next = n5p;
	n5p->next = NULL;
	
	if (head->next != NULL) {//保证链表非空
		//反转
		linklist another = new node;
		another->next = NULL;
		reverseLinkList(head, another);

		//输出
		linklist p = another;
		while (p->next != NULL) {
			cout<<p->next->data<<endl;
			p = p->next;
		}
	}
	return 0;
}
思路三:使用循环实现
#include<iostream>

using namespace std;

typedef struct node
{
	int data;
	struct node* next;
}*linklist,node;

int main() {
	//创建链表,元素为5个
	//这里主要是为了简单,没有使用头插法和尾插法,而是直接构造一个链表
	linklist head = new node;
	linklist n1p = new node;
	linklist n2p = new node;
	linklist n3p = new node;
	linklist n4p = new node;
	linklist n5p = new node;
	n1p->data = 1;
	n2p->data = 2;
	n3p->data = 3;
	n4p->data = 4;
	n5p->data = 5;
	head->next = n1p;
	n1p->next = n2p;
	n2p->next = n3p;
	n3p->next = n4p;
	n4p->next = n5p;
	n5p->next = NULL;
	
	if (head->next != NULL) {//保证链表非空
		linklist plast=nullptr;
		linklist pnow = head->next;
		linklist p=head->next;
		while (p!= nullptr) {
			p = p->next;

			//反转
			pnow->next = plast;
			
			plast = pnow;
			pnow = p;
		}
		//将链表的头结点放在原链表末尾结点之前
		head->next = plast;

		//输出
		p = head;
		while (p->next != nullptr) {
		cout<<p->next->data<<endl;
			p = p->next;
		}
	}
	return 0;
}

这个循环的思路其实我在看完一片递归实现反转链表的博客之后,自己转化之后写的
这个递归实现的博客原文请点击链接https://blog.csdn.net/Rick1860/article/details/82864091

思路四:对原链表使用头删法删除结点,对新链表使用头插法插入链表
#include<iostream>

using namespace std;

typedef struct node
{
	int data;
	struct node* next;
}*linklist,node;

int main() {
	//创建链表,元素为5个
	//这里主要是为了简单,没有使用头插法和尾插法,而是直接构造一个链表
	linklist head = new node;
	linklist n1p = new node;
	linklist n2p = new node;
	linklist n3p = new node;
	linklist n4p = new node;
	linklist n5p = new node;
	n1p->data = 1;
	n2p->data = 2;
	n3p->data = 3;
	n4p->data = 4;
	n5p->data = 5;
	head->next = n1p;
	n1p->next = n2p;
	n2p->next = n3p;
	n3p->next = n4p;
	n4p->next = n5p;
	n5p->next = NULL;
	
	if (head->next != NULL) {//保证链表非空
		linklist another = new node;
		another->next = nullptr;
		linklist p;//用来记录删除结点
		while (head->next != nullptr) {
			//头删法删除
			p = head->next;
			head->next = head->next->next;
			//头插法新增
			p->next = another->next;
			another->next = p;
		}

		//输出
		p = another;
		while (p->next != nullptr) {
		cout<<p->next->data<<endl;
			p = p->next;
		}
	}
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值