首页 > 其他 > 详细

剑指offer 链表中倒数第k个结点

时间:2018-08-25 19:45:44      阅读:125      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        if(pListHead == nullptr) return nullptr;
        int len = 0;
        ListNode* root = pListHead;
        while(pListHead != nullptr){
            len++;
            pListHead = pListHead->next;
        }
        int counter = len - k;
        if(counter < 0) return nullptr;
        while(root != nullptr){
            if(counter == 0) return root;
            root = root->next;
            counter--;
        }
        return nullptr;
    }
};

剑指offer 链表中倒数第k个结点

原文:https://www.cnblogs.com/theodoric008/p/9534989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!