剑指offer-面试题6-从头到尾打印链表-链表

/*
题目:
	输入一个链表的头节点,从尾到头反过来打印每个节点的值
*/
/*
思路:
	解法一:利用栈后进先出的特性。
	解法二:利用递归函数的性质。
*/
	
void PrintListReversingly_Iteratively(ListNode *pHead){
	std::stack<ListNode> nodes;
	ListNode *pNode = pHead;
	
	while(pNode != null){
		nodes.push(pNode);
		pNode = pNode->next;
	}
	
	while(!nodes.empty()){
		pNode = nodes.top();
		printf("%d\t",pNode->value);
		nodes.pop()
	}
}

void PrintListReversingly_Recursively(ListNode *pHead){
	if(pHead != null){
		PrintListReversingly_Recursively(pHead->next);
		printf("%d\t",pHead->value);
	}
}

   

猜你喜欢

转载自www.cnblogs.com/buaaZhhx/p/11808642.html