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

1.移除链表元素

在这里插入图片描述

struct ListNode* removeElements(struct ListNode* head, int val)//移除链表元素
{
	if (head == NULL) return head;
	struct ListNode* cur = head;
	struct ListNode* prev = NULL;
	while (cur)
	{
		struct ListNode* next = cur->next;
		if (cur->val == val)
		{
			if (prev == NULL)
			{
				head = next;
			}
			else
			{
				prev->next = next;
			}
			free(cur);
			cur = next;
		}
		else
		{
			prev = cur;
			cur = next;
		}
	}
	return head;
}

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;
}

方法二:(迭代)

struct ListNode* reverseList(struct ListNode* head)//迭代反转
{
	struct ListNode* prev = NULL;
	struct ListNode* cur = head;
	struct ListNode* next;
	while (cur)
	{
		next = cur->next;
		cur->next = prev;
		prev = cur;
		cur = next;
	}
	return prev;
}

方法三:(压入栈反转)

struct ListNode* reverseList(struct ListNode* head)//取节点式反转
{
	if (head == NULL || head->next == NULL) return head;
	struct ListNode* newhead = NULL;
	while (head != NULL)
	{
		struct ListNode* next = head->next;
		head->next = newhead;
		newhead = head;
		head = next;
	}
	return newhead;
}

3.返回链表中间结点

在这里插入图片描述
方法一:(存放到数组)

struct ListNode* middleNode(struct ListNode* head)//存放在数组中找中间结点
{
	static struct ListNode* a[100] = { 0 };
	int t = 0;
	if (head == NULL || head->next == NULL) return head;
	while (head != NULL)
	{
		a[t] = head;
		head = head->next;
		++t;
	}
	t = t / 2;
	return a[t];
}

方法二:(遍历一边找中间)

struct ListNode* middleNode(struct ListNode* head)
{
	struct ListNode* cur = head;
	int count = 0;
	while(cur)
	{
		count++;
		cur = cur -> next;
	}
	count = count/2;
	cur = head;
	while(count--)
	{
		cur = cur -> next;
	}
	return cur;
}

方法三:(快慢指针)

struct ListNode* middleNode(struct ListNode* head)//快慢指针找中间结点
{
	struct ListNode* fast = head;
	struct ListNode* slow = head;
	while (fast&& fast->next)
	{
		slow = slow->next;
		fast = fast->next->next;
	}

	return slow;
}

4.返回链表的第k个结点

在这里插入图片描述

ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)//返回链表的第k个结点
{
	ListNode* fast = pListHead;
	ListNode* slow = pListHead;
	while (k--)
	{
		if (fast)
		{
			fast = fast->next;
		}
		else
		{
			return NULL;
		}
	}
	while (fast != NULL)
	{
		fast = fast->next;
		slow = slow->next;
	}

	return slow;
}

5.合并俩个有序链表

在这里插入图片描述

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)//合并俩个有序链表 
{
	struct ListNode* head;
	struct ListNode* tail;
	if (l1 == NULL) return l2;
	if (l2 == NULL) return l1;
	if (l1->val < l2->val)
	{
		head = l1;
		l1 = l1->next;
	}
	else
	{
		head = l2;
		l2 = l2->next;
	}
	tail = head;
	while (l1 && l2)
	{
		if (l1->val < l2->val)
		{
			tail->next = l1;
			tail = tail->next;
			l1 = l1->next;
		}
		else
		{
			tail->next = l2;
			tail = tail->next;
			l2 = l2->next;
		}
	}
	if (l1)
	{
		tail->next = l1;
	}
	if (l2)
	{
		tail->next = l2;
	}

	return head;
}

6.链表分割

在这里插入图片描述

    ListNode* partition(ListNode* pHead, int x) {
	ListNode* cur = pHead;
	struct ListNode* lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));
	struct ListNode* greathead = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* ltail = lesstail;
    struct ListNode* ghead = greathead;
	while(cur)
	{
		if(cur -> val < x)
		{
			ltail -> next = cur;
			ltail = ltail -> next;
		}
		else
		{
			ghead -> next = cur;
			ghead = ghead -> next;
		}
		cur = cur -> next;
	}
	ltail -> next = greathead -> next;
	ghead -> next = NULL;
	pHead = lesstail -> next;
        
        return pHead;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值