单链表的基本操作

单链表的插入,反转,判断是否有环,深复制,以及带random指针的单链表的深拷贝

#include<iostream>
using namespace std;
struct ListNode
{
	int val;
	ListNode *next;
	ListNode(int x) :val(x), next(NULL){}
};
//单向链表的反转,三个指针进行操作,很简单
ListNode *reverseList(ListNode *head)
{
	ListNode *p1, *p2, *p3=NULL;
	if (head == NULL || head->next == NULL) return head;
	p1 = head;
	p2 = p1->next;
	while (p2)
	{
		p3 = p2->next;
		p2->next = p1;
		p1 = p2;
		p2 = p3;
	}
	head->next = NULL;
	head = p1;
	return head;
}
//判断链表是不是带环,如果带环,找出环的入口节点
bool hasLoop(ListNode *head, ListNode *intrance)
{
	ListNode *p1, *p2;
	p1 = head, p2 = head;
	if (p1 == NULL) return false;
	bool tag = false;
	while (p1 != NULL && p2 != NULL)
	{
		p1 = p1->next;
		p2 = p2->next->next;
		if (p1 == p2)
		{
			tag = true;
			break;
		}
	}
	if (tag)//说明存在环
	{
		p1 = head;
		while (p1 != p2)
		{
			p1 = p1->next;
			p2 = p2->next;
		}
	}
	intrance = p1;
	return tag;
}
//链表的深拷贝,链表可能有环
ListNode *deepCopy(ListNode *head)
{
	if (!head) return NULL;
	ListNode *intrance = NULL;
	if (hasLoop(head, intrance)) //如果链表有环
	{
		ListNode *p = head;
		ListNode *copy = new ListNode(-1);
		ListNode *q = copy;
		while (p != intrance)
		{
			q->next = new ListNode(p->val);
			p = p->next; 
			q = q->next;
		}
		q->next = new ListNode(intrance->val);
		ListNode *cur = q->next;
		p = intrance->next;
		q = q->next;
		while (p != intrance)
		{
			q->next = new ListNode(p->val);
			p = p->next;
			q = q->next;
		}
		q->next = cur;
		return copy->next;
	}
	else //链表无环
	{
		ListNode *copy = new ListNode(-1);
		ListNode *q = copy;
		ListNode *p = head;
		while (p != NULL)
		{
			q->next = new ListNode(p->val);
			p = p->next;
			q = q->next;
		}
		q->next = NULL;
		return copy->next;
	}
}
//单链表节点的插入
ListNode *insert(ListNode *head, int num)
{
	ListNode *cur = new ListNode(num);
	if (head == NULL)
	{
		return cur;
	}
	ListNode *pre = head;
	ListNode *p=NULL; //记住前驱结点
	while (num > pre->val && pre->next!=NULL)
	{
		p = pre; //记住前驱结点
		pre = pre->next;
	}
	if (num <= pre->val)
	{
		if (pre == head)
		{
			cur->next = pre;
			head = cur;
		}
		else
		{
			cur->next = pre;
			p->next = cur;
		}
	}
	else//该节点应该被插入到最后一个位置
	{
		pre->next = cur;
		cur->next = NULL;
	}
	return head;
}
//带随机节点的链表的深复制
struct RandomListNode
{
	int val;
	RandomListNode *next;
	RandomListNode *random;
	RandomListNode(int x) :val(x), next(NULL), random(NULL){}
};

RandomListNode *copyRandomListNode(RandomListNode *head)
{
	if (!head) return NULL;
	RandomListNode *pre = head;
	//将节点复制放在原来节点的后面
	while (pre)
	{
		RandomListNode *cur = new RandomListNode(pre->val);
		cur->next = pre->next;
		pre->next = cur;
		pre = cur->next;
	}
	pre = head;
	RandomListNode *cur = pre->next;
	while (pre)
	{
		if (pre->random) cur->random = pre->random->next;
		pre = cur->next;
		if (pre) cur = pre->next;
	}
	pre = head;
	cur = pre->next;
	RandomListNode *copy = cur;
	while (cur->next)
	{
		pre->next = cur->next;
		cur->next = cur->next->next;
		pre = pre->next;
		cur = cur->next;
	}
	pre->next = NULL;
	return copy;
}
void print(ListNode *head)
{
	while (head)
	{
		cout << head->val << " ";
		head = head->next;
	}
}
int main()
{
	ListNode *head=NULL;
	for (int i = 1; i < 10; i += 2)
	{
		head = insert(head, i);
	}
	head=reverseList(head);
	print(head);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值