【每日一题-5】复杂链表的复制

逆序打印单链表

void ReversePrintList(listNode* pHead)
{
	if (pHead == NULL)
		return NULL;
	ReversePrintList(pHead->next);
	cout << pHead->next;
}
删除一个无头单链表的非尾节点

void DelNontaiNode(ListNode* pos)
{
	ListNode* cur = NULL;
	cur = pos->next;
	pos->data = cur->data;
	pos->next = cur->next;
	free(cur);
}
复杂链表的复制
typedef struct Node
{
	int data;
	struct Node* next;
	struct Node* random;
}Node,*pNode;

pNode CreateListNode(int data)
{
	pNode newNode = (pNode)malloc(sizeof(Node));
	if (NULL == newNode)
	{
		cout << "not fo memory" << endl;
		exit(1);
	}
	newNode->data = data;
	newNode->next = NULL;
	newNode->random = NULL;
	return newNode;
}

pNode CloneComplexList(pNode pHead)
{
	if (NULL == pHead)
	{
		return NULL;
	}
	pNode cur = pHead;
	pNode pNext = NULL;
	pNode pNew = NULL;

	while (cur)	//复制节点到其后的每一个位置
	{
		pNew = CreateListNode(cur->data);
		pNew->next = cur->next;
		cur->next = pNew;
		cur = pNew->next;
	}

	//节点赋值
	cur = pHead;
	pNext = NULL;
	while (cur)
	{
		pNext = cur->next;
		pNext->random = cur->random;
		cur = pNext->next;
	}
	//拆链
	pNew = pHead->next;
	pNext = pNew;
	cur = pNext->next;
	pHead->next = cur;
	while (cur)
	{
		pNext->next = cur->next;
		pNext = pNext->next;
		cur = pNext->next;
		cur = cur->next;
	}
	return pNew;
}

你可能感兴趣的:(链表面试题,逆序打印,删除无头非尾节点,复杂链表复制,刷题,每日一题)