题解 | #链表中倒数最后k个结点# #两个链表的第一个公共结点# #删除链表的倒数第n个节点#C快慢指针

在这里插入图片描述

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;
    // write code here
}

在这里插入图片描述

struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
    // write code here
    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 ) {
    // write code here
    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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香草绵绵冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值