反转链表

反转链表

分别实现反转单向链表和反转双向链表的函数

要求

若链表长度为n,要求时间复杂度为O(n),空间复杂度为O(1).
反转双链表与单链表的区别只在于一个前向指针的设置问题

代码

#include<iostream>
using namespace std;

struct node {
	int value;
	node* next;
	node(int val)
	{
		value = val;
	}
};
struct dNode {
	int value;
	dNode* last;
	dNode* next;
	dNode(int val) {
		value = val;
	}
};

void printS(node* head)
{
	if (head == NULL)
		cout << "NULL" << endl;
	while (head != NULL)
	{
		cout << head->value << " ";
		head = head->next;
	}
	cout << endl;
}

node* createList(int* in, int len)
{
	node* head = new node(in[0]);
	node* p = head;
	for (int i = 1; i < len; i++)
	{
		node* pNode = new node(in[i]);
		p->next = pNode;
		p = p->next;
	}
	p->next = NULL;
	return head;
}

node* reverse(node* pHead)
{
	if (pHead == NULL || pHead->next == NULL)
		return pHead;
	node* newHead = NULL;
	node* prev = NULL;
	node* cur = pHead;
	node* next = NULL;
	while (cur != NULL)
	{
		next = cur->next;
		if (next == NULL)
			newHead = cur;
		cur->next = prev;
		prev = cur;
		cur = next;/*上面两句顺序反了会有问题*/
		
	}
	return newHead;
}

dNode* reverseD(dNode* pHead)
{
	if (pHead == NULL || pHead->next == NULL)
		return pHead;
	dNode* prev = NULL;
	dNode* next = NULL;
	while (pHead != NULL)
	{
		next = pHead->next;
		pHead->next = prev;
		pHead->last = next;  //相比单链表的反转多出了前向指针的设置
		prev = pHead;
		pHead = next;
	}
	return prev;
}

int main()
{
	int input[] = { 1, 2, 3, 4 };
	int len = 4;
	node* pHead = createList(input, len);
	node* newH = reverse(pHead);
	printS(newH);
	getchar();
	return 0;
}

反转部分链表

给定一个单向链表的头节点head,以及两个整数from和to,在单向链表上把from个节点到第to个节点的这一部分进行反转。

要求

  • 如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度为O(1).
  • 如果不满足1<=from<=to<=N,则不需调整

几个关键点

  • 判断是否满足1<=from<=to<=N,不满足则直接返回原头节点
  • 找到反转部分的前一个节点和后一个节点保存,将反转部分反转之后再进行连接
  • 如果fpre为NULL,那么说明反转部分包含头节点,否则要返回旧的头节点

代码

#include<iostream>
using namespace std;

struct node {
	int value;
	node* next;
	node(int val)
	{
		value = val;
	}
};

node* createList(int* in, int len)
{
	node* head = new node(in[0]);
	node* p = head;
	for (int i = 1; i < len; i++)
	{
		node* pNode = new node(in[i]);
		p->next = pNode;
		p = p->next;
	}
	p->next = NULL;
	return head;
}

void printList(node* head)
{
	if (head == NULL)
		cout << "NULL" << endl;
	cout << "Whole list:" << endl;
	while (head != NULL)
	{
		cout << head->value << " ";
		head = head->next;
	}
	cout << endl;
}

node* reversePart(node* pHead, int from, int to)
{
	if (pHead == NULL || pHead->next == NULL)
		return pHead;
	int len = 0;
	node* pNode = pHead;
	node* fPre = NULL;
	node* tPos = NULL;
	/*===================================================*/
	while (pNode != NULL)
	{
		len++;
		fPre = len == from - 1 ? pNode : fPre;
		tPos = len == to + 1 ? pNode : tPos;
		pNode = pNode->next;
	}
	/*==================================================*/
	if (from > to || from < 1 || to > len)
		return pHead;
	/****************************************************/
	pNode = fPre == NULL ? pHead : fPre->next;
	node* pNode2 = pNode->next;//反转部分与反转整个链表区别不大
	pNode->next = tPos;       //主要是之前的这一些确定位置的操作
	node* next = NULL;
	/****************************************************/
	while (pNode2 != tPos)
	{
		next = pNode2->next;
		pNode2->next = pNode;
		pNode = pNode2;
		pNode2 = next;
	}
	if (fPre != NULL)
	{
		fPre->next = pNode;
		return pHead;
	}
	return pNode;
}

int main()
{
	int input[] = { 1, 2, 3, 4, 5 };
	int len = 5;
	node* head = createList(input, len);
	int from = 2;
	int to = 4;
	node* newH = reversePart(head, from, to);
	printList(newH);
	getchar();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值