链表

定义节点:

typedef struct ListNode
{
      int        m_nKey;
      ListNode*  m_pNext;
}ListNode;


1.链表反转

ListNode* Reverse(ListNode* pHead)
{
	ListNode* curNode = pHead;
	ListNode* nextNode = pHead->m_pNext;
	ListNode* endNode = NULL;

	if (pHead == NULL || pHead ->m_pNext == NULL)
	{
		return pHead;
	}

	curNode->m_pNext = NULL;//this is new tail node ,m_pNext pointer must be NULL
	while (nextNode)
	{
		endNode = nextNode->m_pNext;
		nextNode->m_pNext = curNode;
		curNode = nextNode;
		nextNode = endNode;
	}

	return curNode;//new head
}


2.查找链表中倒数第k个结点

ListNode * ListLastK(ListNode* head,unsigned int K)
{
	ListNode *slow = NULL;
	ListNode *fast = NULL;

	if (K<1 || head == NULL)
	{
		return NULL;
	}
	
	while (K-- && fast != NULL)
	{
		fast = fast->m_pNext;
	}
	if (K > 0)
	{
		return NULL;//list len < K
	}
	while (fast != NULL)
	{
		fast = fast->m_pNext;
		slow = slow->m_pNext;
	}
	return slow;
}



3.取链表中位节点

(1)如果是偶数个节点,取前中位节点

ListNode * ListMid(ListNode* head)
{
	ListNode* fastNode = head;
	ListNode* slowNode = head;
	

	while (fastNode != NULL && fastNode->m_pNext != NULL)
	{
		slowNode = slowNode->m_pNext;
		fastNode = fastNode->m_pNext->m_pNext
	}
	return slowNode;//return pre-Mid node
}


(2)如果是偶数个节点,取后中位节点

ListNode *ListMid(ListNode* head)
{
	ListNode* fastNode = head;
	ListNode* slowNode = head;
	
	
	while (fastNode != NULL && fastNode->m_pNext != NULL)
	{
		slowNode = slowNode->m_pNext;
		fastNode = fastNode->m_pNext->m_pNext
	}
	if (head != NULL && fastNode = NULL)
	{
		slowNode = slowNode->m_pNext;
	}
	return slowNode;//return atfer-Mid node
}


4.两个单向链表,有可能交叉,判断是否交叉,如果交叉,返回第一个交叉点,否则返回NULL

ListNode* intercross(ListNode* firstList,ListNode* secondList)
{
	ListNode *tempNode = NULL;
	ListNode *firstNode = firstList;
	ListNode *secondeNode = secondList;
	unsigned int listLen1 = 0;
	unsigned int listLen2 = 0;
	
	int decLen = 0;

	if (firstList == NULL && secondList == NULL)
	{
		return NULL;
	}
	tempNode = firstList;
	while (tempNode != NULL)
	{
		++listLen1;
		tempNode->m_pNext;
	}
	tempNode = secondList;
	while(tempNode != NULL)
	{
		++listLen2;
		tempNode->m_pNext;
	}
	decLen = listLen1-listLen2;

	if (decLen>=0)
	{
		if (decLen-- > 0)
		{
			firstNode = firstNode->m_pNext;
		}	
	}
	else
	{
		if (decLen++ <0)
		{
			secondeNode = secondeNode->m_pNext;
		}
	}

	while (firstNode != NULL)
	{
		if (firstNode == secondeNode)
		{
			return firstNode;
		}
	}
	return NULL//does not find the same node ,so return NULL
}



5.判断链表是否有环,如果有,返回进入环的第一个节点

ListNode * Listloop(ListNode* head)
{
	char isLoop = 0;
	ListNode* slow = NULL;
	ListNode* fast = NULL;
	ListNode* newHead = NULL;
	ListNode* crossNode = NULL;

	slow = fast = head;
	while (fast != NULL && fast->m_pNext != NULL)
	{
		fast = fast->m_pNext->m_pNext;
		slow = slow->m_pNext;
		if (fast == slow)
		{
			isLoop = 1;
			break;//this is loop list
		}
	}
	//here we can see the list is go end then is not loop or is loop because isloop is 1
	if (isLoop == 1)
	{
		//find the entry node

		//break the loop,then we have two list ,find the intercross node ,that's what we want want
		newHead = slow->m_pNext;
		slow->m_pNext = NULL;//break the loop
		
		crossNode = intercross(head,newHead);
		slow->m_pNext = newHead;//connect the two list ,make it loop like before

		return crossNode;
	}	
	else
	{
		return NULL;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值