【反转链表+vector】
/**
Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/
class Solution {
public:
vector reversePrint(ListNode head) {
ListNode* pre=nullptr;//定义一个虚拟头节点
ListNode* cur=head;
ListNode* temp;
while(cur){
temp=cur->next;//暂存
cur->next=pre;//反转
pre=cur;//前进
cur=temp;//前进
}
vector v;
while(pre){
v.push_back(pre->val);
pre=pre->next;
}
return v;
}
};
上一篇:Redis原理篇(二)网络模型
下一篇:初始网络编程