/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead==NULL)return NULL;
ListNode* p=pListHead;
ListNode* q=pListHead;
while(k-->0){
p=p->next;
if(p==NULL && k==0) return pListHead; //链表长度等于k
if(p==NULL)return NULL; //链表长度小于k
}
while(p!=NULL){
p=p->next;
q=q->next;
}
return q;
/*
if(pListHead==NULL)return NULL;
int count=0;
ListNode* p=pListHead;
while(p!=NULL){
p=p->next;
count++;
}
if(k>count)return NULL;
else{
p=pListHead;
int i=0;
while(i < count-k){
p=p->next;
i++;
}
return p;
}
*/
}
};
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL)return NULL;
if(pHead->next == NULL)return pHead;
if(pHead->next->next == NULL){
ListNode *p = pHead->next->next;
pHead->next->next = pHead->next;
pHead->next = NULL;
return p;
}
ListNode *l, *p, *r;
l = pHead;
p = pHead->next;
r = pHead->next->next;
while(r != NULL){
p->next = l;
l=p;
p=r;
r=r->next;
}
p->next = l;
pHead -> next = NULL;
return p;
}
};