(栈)
#include <stack>
//
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
stack<int> s;
while(head != NULL){
s.push(head->val);
head = head->next;
}
while(!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}
};
int Fibonacci(int n) {
// write code here
int a = 1, b = 1, c = 1;
for(int i = 3; i <= n; i ++){
c = a + b;
a = b;
b = c;
}
return c;
}
//从子问题开始解答 递归是从顶部开始 动态规划是从底下开始
int Fibonacci(int n) {
// write code here
if(n == 0){
return 0;
}
if(n == 1){
return 1;
}
int res = 0;
int a = 0, b = 1;
for(int i = 2; i <= n; i++){
res = a + b;
a = b;
b = res;
}
return res;
}
初始化一个返回链表
前序节点
当前节点
当前序节点的next指向当前节点的next,相当于将当前的值删除
遍历链表
while(cur != NULL){
cur = cur->next;
}