欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

链表进行了翻转

程序员文章站 2022-03-11 16:36:07
...
ListNode* ReverseIteratively(ListNode* pHead)//本身就把链表进行了翻转,按顺序打印就是翻转后链表的元素
{
	ListNode* pReversedHead = NULL;
	ListNode* pNode = pHead;
	ListNode* pPrev = NULL;
	while(pNode != NULL)
	{
		// get the next node, and save it at pNext
		ListNode* pNext = pNode->m_pNext;

		// if the next node is null, the currect is the end of original 
		// list, and it's the head of the reversed list
		if(pNext == NULL)
			pReversedHead = pNode;

		// reverse the linkage between nodes
		pNode->m_pNext = pPrev;

		// move forward on the the list
		pPrev = pNode;
		pNode = pNext;
	}

	return pReversedHead;
}