题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=295&tqId=23286&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
//vector数组存储+反向赋值
ListNode* ReverseList(ListNode* head) {
// write code here
if(!head || !head->next) return head;
ListNode* p,*r;
p=head;
int n=0;
vector<int> vec;
while(p)
{
n++;
vec.push_back(p->val);
p=p->next;
}
p=head;
for(int i=n-1;i>=0;i--)
{
p->val=vec[i];
p=p->next;
}
return head;
}
};