![](https://i-blog.csdnimg.cn/blog_migrate/ed6f51afdb69d2e47f54a941b6b4619b.png)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<int> reverseBookList(ListNode* head) {
stack<int> st;
vector<int> v;
while(head)
{
st.push(head->val);
head = head->next;
}
while(!st.empty())
{
v.push_back(st.top());
st.pop();
}
return v;
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/5a4dafc0ded288eaf655202a6be36f77.png)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* trainingPlan(ListNode* head, int cnt) {
ListNode* tmp = head;
int i = 0;
while(tmp)
{
tmp = tmp->next;
i +=1;
}
for(int j = 0;j<i-cnt;j++)
{
head = head->next;
}
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* trainingPlan(ListNode* head, int cnt) {
ListNode* slow = head;
ListNode* fast = head;
while(cnt)
{
fast = fast->next;
cnt--;
}
while(fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};