0
点赞
收藏
分享

微信扫一扫

06链表逆转

洲行 2022-01-20 阅读 44
#include<Windows.h>
#include<iostream>
using namespace std;
//翻转链表

typedef struct node
{
	int a;
	struct node*next;
}node;

int main()
{
	//初始化一个链表
	node* node0 = new node; node0->a = 1;
	node* node1 = new node; node1->a = 2;
	node* node2 = new node; node2->a = 3;
	node* node3 = new node; node3->a = 4;
	node0->next = node1; node1->next = node2; node2->next = node3; node3->next = NULL;
	// 逆转
	node* head=new node,*p,*q;
	p = node0;
	head->next = NULL;
	// 断成两个链表
	// head->NULL

	// node0->node1->node2->node3->NULL;
	//  p		q
	while (p!=NULL)
	{
		q = p->next;
		p->next = head->next;
		head->next = p;
		p = q;
	}
	cout << head->next->a << head->next->next->a << head->next->next->next->a << head->next->next->next->next->a << endl;
}
举报

相关推荐

0 条评论