从未到头打印链表

#include<iostream>
#include<stack>
typedef struct ListNode
{
	int value;
	struct ListNode *next;
	ListNode(int v) :value(v), next(NULL)
	{}
};
void PrintListReverseingly(ListNode *head)
{
	std::stack<ListNode*> s;
	ListNode *pHead = head;
	if (pHead != nullptr)
	{
		while (pHead)
		{
			s.push(pHead);
			pHead = pHead->next;

		}
		while (!s.empty())
		{
			pHead = s.top();
			std::cout << pHead->value << " ";
			s.pop();
		}
	}

}
int main(void)
{
	ListNode n1(1);
	ListNode n2(2);
	ListNode n3(3);
	ListNode n4(4);
	ListNode n5(5);
	 n1.next = &n2;
	 n2.next = &n3;
	 n3.next = &n4;
	 n4.next = &n5;
	 ListNode *head = &n1;
	 PrintListReverseingly(head);
	 std::cout << std::endl;


	system("pause");

	return 0;
}

 

上一篇:*链表中环的入口结点


下一篇:【剑指offer】链表中环的入口结点(链表)