struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) {
if(pHead==NULL)
return NULL;
struct ListNode* fast=pHead;
struct ListNode* slow=pHead;
int i=0;
while(fast!=NULL)
{
i++;
fast=fast->next;
if(i==k)
break;
}
if(fast==NULL&&i!=k)
return NULL;
if(fast==NULL&&i==k)
return slow;
while(fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}
return slow;
}
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
struct ListNode* H=(struct ListNode *)malloc(sizeof(struct ListNode));
H->next=head;
struct ListNode* cur=H;
struct ListNode *fast=H,*slow=H;
int i=0;
while(i!=n+1)
{
fast=fast->next;
i++;
}
while(fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;
return H->next;
}
struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) {
if(pHead1==NULL||pHead2==NULL)
return NULL;
struct ListNode *p1=pHead1,*p2=pHead2;
int i=0,j=0;
while(1)
{
if(p1==p2)
return p1;
p1=p1->next;
p2=p2->next;
if(p1==NULL)
{
p1=pHead2;
i++;
if(i==2)
return NULL;
}
if(p2==NULL)
{
p2=pHead1;
j++;
if(j==2)
return NULL;
}
}
}