//翻转倒数第k个结点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
int n = 0;
for (auto p = pListHead; p; p = p->next) n ++ ;
if (n < k) return nullptr;
auto p = pListHead;
for (int i = 0; i < n - k; i ++ ) p = p->next;
return p;
}
};
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
int n = 0;
for (auto p = pListHead; p; p = p->next) n ++ ;
if (n < k) return nullptr;
auto p = pListHead;
for (int i = 0; i < n - k; i ++ ) p = p->next;
return p;
}
};
2020-05-07
在牛客打卡15天,今天学习:刷题 1 道/代码提交 1 次
全部评论
相关推荐

点赞 评论 收藏
分享