算法学习07:反转单向和双向链表

这篇博客介绍了如何在O(N)时间和O(1)空间复杂度内反转单向和双向链表。提供了详细的代码实现,包括反转单链表、双向链表的插入、删除和打印链表的操作。通过交换节点的前后指针完成双向链表的反转,对于单链表则是通过迭代方式更新节点指针来实现反转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

分别实现反转单向链表和反转双向链表的函数。要求:如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度为O(1)

反转单链表分析

用三个指针分别记录前一个节点temp,当前节点pre和后一个节点phead。循环中,将temp后移到pre的位置,pre后移到phead的位置,phead移动到下一个节点,并将当前节点pre的指针域指向前一个节点temp。当phead到达链表末尾时,即phead-next=null的时候,当前的末节点是pre,将其指针域指向temp。恰好完成整个链表的反转,循环结束,返回反转后链表的头结点pre

link InvertList(link head){
	link pre,phead,temp;
	phead = head;  //将phead指向链表头,做游标使用
	pre = NULL;  //pre为头指针之前的节点
	while(phead != NULL){
		temp = pre;
		pre = phead;
		phead = phead->next;
		pre->next = temp;  //pre接到之前的节点 
	}
	return pre; 
}


在这里插入图片描述
完整代码

#include<iostream>
#include<cstdlib>
using namespace std;

class list {
public:
	int data;
	class list* next;
};
typedef class list node;
typedef node* link;

link FindNode(link head, int position_data) {
	link phead;
	phead = head;
	while (phead != NULL) {
		if (phead->data == position_data)return phead;
		phead = phead->next;
	}
	return phead;
}

link InsertNode(link head, int position_data, int data) {
	link phead = new node;
	phead = FindNode(head, position_data);
	link insertnode = new node;
	if (!insertnode) return NULL;
	insertnode->data = data;
	insertnode->next = NULL;
	if (phead == NULL) {  //插入第一个节点
		insertnode->next = head;
		return insertnode;
	}
	else if (phead->next == NULL) phead->next = insertnode;  //插入最后一个节点
	else {  //插入中间节点 
		insertnode->next = phead->next;
		phead->next = insertnode;
	}
	return head;
}

link DeleteNode(link head, int position_data) {
	link top = head;  //保留头指针 
	link phead = FindNode(head, position_data);
	if (head == phead) {  //删除头结点 
		head = head->next;
		delete phead;
	}
	else {
		while (top->next != phead) top = top->next;
		if (phead->next == NULL) {  //删除尾结点 
			top->next = NULL;
			delete phead;
		}
		else {
			top->next = phead->next;
			delete phead;
		}
	}
	return head;
}

link InvertList(link head) {
	link pre, phead, temp;
	phead = head;  //将phead指向链表头,做游标使用
	pre = NULL;  //pre为头指针之前的节点
	while (phead != NULL) {
		temp = pre;
		pre = phead;
		phead = phead->next;
		pre->next = temp;  //pre接到之前的节点 
	}
	return pre;
}

link CreateList(int a[], int n) {
	link head, phead, newnode;
	phead = new node;
	if (!phead) return NULL;
	phead->data = a[0];
	head = phead;
	for (int i = 1; i < n; i++) {
		newnode = new node;
		newnode->data = a[i];
		newnode->next = NULL;
		phead->next = newnode;
		phead = phead->next;
	}
	return head;
}

void PrintList(link head) {
	link phead = new node;
	phead = head;
	cout << "链表元素如下: " << endl;
	while (phead != NULL) {
		cout << phead->data << "->";
		head = phead;
		phead = phead->next;  //phead按序往后遍历整个链表
		if (!phead) cout << "NULL" << endl;
	}
}

int main() {
	int position_data, data;
	link head, phead;
	int n=5;
	cout << "请输入初始链表元素个数: " << endl;
	//cin >> n;
	int a[5];
	cout << "请依次输入链表元素: ";
	for (int i = 0; i < n; i++) cin >> a[i];
	head = CreateList(a, n);
	PrintList(head);
	cout << "请输入预插入位置之前的元素和要插入的元素(例:5 8): ";
	cin >> position_data >> data;
	head = InsertNode(head, position_data, data);
	cout << "插入之后的";
	PrintList(head);
	cout << "请输入想删除的链表元素: ";
	cin >> position_data;
	head = DeleteNode(head, position_data);
	cout << "删除之后的";
	PrintList(head);
	cout << "反转之后的";
	head = InvertList(head);
	PrintList(head);
	return 0;
}


反转双链表

双向链表的反转,就是交换每个节点的前向指针和后向指针,然后调整链表的头指针和尾指针。
构建双向链表节点,与单链表节点相比,多了一个指向前一个元素的指针域。

struct Node{
	int data;
	Node* next;
	Node* prev;
	};

完整代码

#include <iostream>
 
struct Node
{
	int data;
	Node *next; // 指向下一个节点
	Node *prev; // 指向前一个节点
};
 
//对链表进行反转
void reverse(Node **head)
{
	Node *temp = NULL;
	Node *current = *head;
 
	//交换每个节点的后向指针和前向指针
	// 1-->2-->3, 假设2为current.
	while (current != NULL)
	{
		temp = current->prev; //temp=1
		current->prev = current->next; //3-->2
		current->next = temp;            //2-->1 
		current = current->prev; //3-->2-->1, current变为3,继续往后循环。
	}
	//总结:先处理前向指针,然后处理后向指针。这些操作都只对当前节点(current),不涉及其它节点。
	//1.缓存前向指针
	//2.将后向指针赋值给前向指针
	//3.将缓存的前者指针,赋值给后向指针
	//4.当前节点指针移动到下一个待处理节点
 
	//修改头指针之前,先检测链表是否为空链表,或者只有一个节点的情况
	if (temp != NULL)
		*head = temp->prev;
}
 
// 给定链表的头指针(head)以及一个整数,插入一个新的节点至链表的头部  
// 之所以传入双指针,因为函数中需要修改链表  
void push(Node** head, int newData)
{
	//1. 分配新节点内存  
	Node* newNode = new Node;
 
	//2. 赋值  
	newNode->data = newData;
 
	//3. 将原始头节点做为新节点的后向指针,而前向指针置为NULL  
	newNode->next = (*head);
	newNode->prev = NULL;
 
	//4. 将原始头节点的前向指针置为新的节点  
	if ((*head) != NULL)
		(*head)->prev = newNode;
 
	//5. 将头指针置为新的节点  
	(*head) = newNode;
}
 
void printList(Node *head)
{
	while (head != NULL)
	{
		std::cout<<" "<<head->data<<" ";
		head = head->next;
	}
	std::cout << std::endl;
}
 
 
int main()
{
	//初始化链表为:10<->8<->6<->4<->2<->0
	Node* head = NULL;
	push(&head, 0);
	push(&head, 2);
	push(&head, 4);
	push(&head, 6);
	push(&head, 8);
	push(&head, 10);
 
	std::cout << "Original DLL is: ";
	printList(head);
 
	reverse(&head);
 
	std::cout << "Reversed DLL is: ";
	printList(head);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值