标签:tno turn return next off shu null problems ptr
链接:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* getKthFromEnd(ListNode* head, int k) { int n = 0; for (ListNode* p = head; p; p = p->next) n++; if (k > n) return nullptr; auto p = head; for (int i = 0; i < n - k; ++i) p = p->next; return p; } };
标签:tno turn return next off shu null problems ptr
原文地址:https://www.cnblogs.com/clown9804/p/12342010.html