我的解题:
使用双指针,先设置一个头结点指向head
/**
* 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) {
ListNode* a=new ListNode(-1);
a->next=head;
ListNode* i=a;
ListNode* j=a;
int count=0;
while(j){
if(count<k){
j=j->next;
count++;
}
else{
j=j->next;
i=i->next;
}
}
return i;
}
};