输入一个链表,反转链表后,输出新链表的表头。
分析:可以利用栈来做,其实递归也是一个栈,叫做递归栈
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(pHead==NULL||pHead->next==NULL) return pHead; ListNode *root=ReverseList(pHead->next); pHead->next->next=pHead; pHead->next=NULL; return root; } };