c++链表获取长度,链表翻转,查找链表倒数第K个节点以及中间点

本文介绍了如何使用C++进行链表操作,包括计算链表的长度、翻转链表、找到单链表中的倒数第K个节点(K大于0)以及定位链表的中间节点。详细阐述了各个操作的实现过程和测试数据。

c++链表获取长度,链表翻转, 查找链表倒数第K个节点以及中间点

测试数据

ListNode* pHead = new ListNode();
	pHead->m_key = 1;
	ListNode* pNode = pHead;
	for (int i = 2; i <= 5; i++)
	{
		ListNode* pNew = new ListNode();
		pNew->m_key = i;
		pNew->pNext = nullptr;
		pNode->pNext = pNew;
		pNode = pNew;
	}

	

	pHead = reverseList(pHead);

	for (ListNode* pNode = pHead; pNode != nullptr; pNode = pNode->pNext)
	{
		cout << pNode->m_key << endl;
	}

1:获得链表的长度

struct ListNode
{
	int m_key;//数据域
	ListNode* pNext;//指针域
};

//求单链表中结点的个数
unsigned int getListLength(ListNode * pHead)
{
	//判断头结点是否为空
	if (pHead == nullptr)
		return 0;

	unsigned int Length=0;
	ListNode* pNode = pHead;
    
    //遍历链表
	while (pNode != nullptr)
	{
		Length++;
		pNode = pNode->pNext;
	}
	return Length;
}

2:链表翻转

//将单链表进行翻转
ListNode* reverseList2(ListNode* pHead)
{
	if (pHead == nullptr||pHead->pNext==nullptr)
		return pHead;

	ListNode* pReverseHead=nullptr;//翻转的头结点
	ListNode* pCurrent = pHead;//当要插入翻转链表的节点
	while (pCurrent != nullptr)
	{
		ListNode* pTemp = pCurrent->pNext;//记录下一个节点
		pCurrent->pNext = pReverseHead;//新节点的下一个节点就是头结点
		pReverseHead = pCurrent;//新头结点变为pCurret
		pCurrent = pTemp;//下一个插入的节点
	}

	return  pReverseHead;
}

3:查找单链表中倒数第K个节点(k>0)

//查找单链表中到处的第K个节点(k>0)
//(前后指针:先让前面的指针走到正向第K个节点,距离最后一个节点(n-k),前后指针同时移动,当指针走到最后一个节点时,后指针刚好走到倒数K个节点
ListNode* Rfundnumber(ListNode* pHead,int k)
{
    if (pHead == nullptr || k == 0)
		return nullptr;

	//定义前后指针
	ListNode* pAhead = pHead;
	ListNode* pBhead = pHead;

	//前指针移动到第K个节点(k-1)
	while(k > 1 && pAhead != nullptr)
	{
		k--;
		pAhead = pAhead->pNext;
	}

	//判断倒数第K个节点不存在的情况
	if (nullptr == pAhead)
		return nullptr;

	//前后指针同时移动,当最后一个指针指向最后一个时
	while (pAhead->pNext != nullptr)
	{
		pAhead = pAhead->pNext;
		pBhead = pBhead->pNext;
	}
	return pBhead;



}

4:查找链表中间节点

/查找中间节点
//前后指针,前指针每次走两步,后指针每次走一步,前指针指向最后一个节点是的时候,后指针就是中间节点
ListNode* getMiddleNode(ListNode* pHead)
{
     //判断pHead是否为空或者只有一个结点
	if (nullptr == pHead || pHead->pNext == nullptr)
	{
		return pHead;
	}

	//定义前后指针
	ListNode* pAhead = pHead;
	ListNode* pBehind = pHead;

	while (pAhead->pNext != nullptr) //指向最后一个结点时,跳出循环
	{
		pAhead = pAhead->pNext;
		

		if (pAhead->pNext != nullptr)
		{
			pAhead = pAhead->pNext;
		}
		pBehind = pBehind->pNext;
	}

	return pBehind;
	

}

//测试数据
int main(int argc,const char* argv[])
{
	ListNode* pHead = new ListNode();
	pHead->m_key = 1;
	ListNode* pNode = pHead;
	for (int i = 2; i <= 5; i++)
	{
		ListNode* pNew = new ListNode();
		pNew->m_key = i;
		pNew->pNext = nullptr;
		pNode->pNext = pNew;
		pNode = pNew;
	}

	ListNode* pHead1 = new ListNode();
	pHead1 = Rfundnumber(pHead,3);
	if (pHead1 == nullptr)
		cout << "没有找到" << endl;
	 else
		cout << pHead1->m_key << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值