题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ #include <cstddef> #include <stack> class Solution { public: ListNode* ReverseList(ListNode* pHead) { stack<int> reverse; ListNode* i = pHead; while(i != NULL){ reverse.push(i->val); i = i->next; } i = pHead; while(i != NULL){ i->val = reverse.top(); i = i->next; reverse.pop(); } return pHead; } }; /* 栈结构是反转顺序的最佳选择,所以本题确实是十分简单,直接用栈反转一次就行,不过空间复杂度略高一点, 不知道有没有不需要辅助空间并且在一次循环内解决的方法,望各位大佬指教 */#23届找工作求助阵地#