单链表基础面试题(下篇)

1.删除链表中的重复结点

在这里插入图片描述

ListNode* deleteDuplication(ListNode* pHead)//删除链表中重复的结点
{
	struct ListNode* cur = pHead;
	struct ListNode* prev = NULL;
	struct ListNode* next = cur->next;
	if (cur == NULL || cur->next == NULL) return pHead;
	while (next)
	{
		if (next->val == cur->val)
		{
			next = next->next;
			while (next)
			{
				if (next->val != cur->val)
				{
					break;
				}
				next = next->next;
			}
			while (cur != next)
			{
				struct ListNode* tmp = cur;
				cur = cur->next;
				free(tmp);
			}
			if (prev == NULL)
			{
				pHead = cur;
			}
			else
			{
				prev->next = next;
			}
			if (next)
			{
				next = next->next;
			}
		}
		else
		{
			prev = cur;
			cur = next;
			next = next->next;
		}
	}
	return pHead;
}

2.回文链表

在这里插入图片描述

struct ListNode* reverselist(struct ListNode* head)//判断一个链表是否为回文链表
{
	if (head == NULL || head->next == NULL) return head;
	struct ListNode* h = reverselist(head->next);
	head->next->next = head;
	head->next = NULL;

	return h;
}
bool isPalindrome(struct ListNode* head) {
	if (head == NULL || head->next == NULL) return true;
	struct ListNode* fast = head;
	struct ListNode* slow = head;
	while (fast && fast->next)
	{
		fast = fast->next->next;
		slow = slow->next;
	}
	slow = reverselist(slow);

	while (slow)
	{
		if (slow->val != head->val)
		{
			return false;
		}
		slow = slow->next;
		head = head->next;
	}

	return true;
}

3.相交链表

在这里插入图片描述

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
	int lenA = 0,lenB = 0,gap = 0;
	struct ListNode* a = headA;
	struct ListNode* b = headB;
	struct ListNode* meet = NULL;
	while(a != NULL && a -> next != NULL)
	{
		lenA++;
		a = a -> next;
	}
	while(b != NULL && b -> next != NULL)
	{
		lenB++;
		b = b -> next;
	}
	if(b != a)
	{
		return NULL;
	}
	else
	{
		a = headA;b = headB;
		if(lenA > lenB)
		{
			gap = lenA - lenB;
			while(gap--)
			{
				a = a -> next;
			}
		}
		else
		{
			gap = lenB - lenA;
			while(gap--)
			{
				b = b -> next;
			}
		}
		while(a && b)
		{
			if(a == b)
			{
				meet = a;
				return a;
			}
			a = a -> next;
			b = b -> next;
		}
	}
	return meet;
}

4.环形链表

在这里插入图片描述

bool hasCycle(struct ListNode *head) //判断是否有环
{
	if (head == NULL || head->next == NULL) return false;
	struct ListNode* slow = head;
	struct ListNode* fast = head->next;
	while (slow != fast)
	{
		if (fast == NULL || fast->next == NULL) return false;
		slow = slow->next;
		fast = fast->next->next;
	}
	return true;
}

5.环形链表||

在这里插入图片描述

struct ListNode *hasCycle(struct ListNode *head) {//寻找相交结点
	if (head == NULL) return head;
	struct ListNode* slow = head;
	struct ListNode* fast = head->next;
	while (slow != fast)
	{
		if (fast == NULL || fast->next == NULL)
		{
			return NULL;
		}
		slow = slow->next;
		fast = fast->next->next;
	}
	return fast;
}
struct ListNode *detectCycle(struct ListNode *head) {
	struct ListNode* meet = hasCycle(head);
	if (meet == NULL)
	{
		return NULL;
	}
	else
	{
		meet = meet->next;
		while (1)//一点有相交节点所以可以写成1
		{
			if (meet == head)
			{
				return meet;
			}
			meet = meet->next;
			head = head->next;
		}
	}
	return;
}

6.拷贝带随机指针的链表

在这里插入图片描述

typedef struct RandomListNode RLNode;
struct RandomListNode *copyRandomList(struct RandomListNode *head)//复制带随机指针的链表
{
	if (head == NULL) return head;
	RLNode* cur = head;
	while (cur)
	{
		RLNode* next = cur->next;
		RLNode* copy = (RLNode*)malloc(sizeof(RLNode));
		copy->label = cur->label;
		cur->next = copy;
		copy->next = next;

		cur = next;
	}
	cur = head;
	while (cur)
	{
		RLNode* copy = cur->next;
		if (cur->random)
		{
			copy->random = cur->random->next;
		}
		else
		{
			copy->random = NULL;
		}

		cur = copy->next;
	}
	RLNode* newhead, *tail;
	newhead = tail = (RLNode*)malloc(sizeof(RLNode));
	cur = head;
	while (cur)
	{
		RLNode* copy = cur->next;
		RLNode* next = copy->next;

		tail->next = copy;
		tail = tail->next;

		cur->next = next;
		cur = next;
	}
	RLNode* Nhead = newhead->next;
	free(newhead);

	return Nhead;
}

以上题目只是博主整理了单链表面试题,各题的详细讲解在之前博客有详细解答
单链表基础面试题(上篇):https://blog.csdn.net/lucky52529/article/details/86481201

猜你喜欢

转载自blog.csdn.net/lucky52529/article/details/86481352