`
java-mans
  • 浏览: 11421827 次
文章分类
社区版块
存档分类
最新评论

链表进行了翻转

 
阅读更多
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;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics