. - 力扣(LeetCode)
class Solution {
public:
ListNode* trainningPlan(ListNode* head, int cnt) {
int n = 0;
ListNode* node = nullptr;
for (node = head; node; node = node->next) {
n++;
}
for (node = head; n > cnt; n--) {
node = node->next;
}
return node;
}
};
class Solution {
public:
ListNode* trainningPlan(ListNode* head, int cnt) {
ListNode* fast = head;
ListNode* slow = head;
while (fast && cnt > 0) {
fast = fast->next;
cnt--;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
};