解题思路,倒数第K个点,位于正数N-K+1的位置。
/*** 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(auto p=head;p;p=p->next)n++;if(k>n)return NULL;auto p=head;for(int i=0;i<n-k;i++)p=p->next;return p;}
};