代码随想录算法训练营第四天| ● 24. 两两交换链表中的节点 ● 19.删除链表的倒数第N个节点 ● 面试题 02.07. 链表相交 ● 142.环形链表II

本文介绍了LeetCode中的四个链表问题,包括两两交换链表节点、删除链表倒数第N个节点、链表相交和环形链表II的解决方案,重点讲解了使用虚拟头节点、快慢指针等技巧来解决这些问题。
摘要由CSDN通过智能技术生成

1.29补1.27的卡

24. 两两交换链表中的节点 - 力扣(LeetCode)

帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点_哔哩哔哩_bilibili

第一眼思路:有思考出交换顺序,可是没有想到如何将切断的点储存起来,以及循环判断条件

cur->next!=NULL是判断偶数节点的终止条件,cur->next->next!=NULL是判断奇数节点的。

cur->next!=NULL&&cur->next->next!=NULL要同时满足(奇数个节点就不用交换了),所以只有后面满足偶数个节点时才进入循环

本题通过引入一个虚拟头结点完成:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *dummynode=new ListNode;
        ListNode *temp,*temp1,*cur;
        dummynode->next=head;
        cur=dummynode;
        while(cur->next!=NULL&&cur->next->next!=NULL)
        {
            temp=cur->next;
            temp1=cur->next->next->next;
            cur->next=cur->next->next;
            cur->next->next=temp;
            temp->next=temp1;
            cur=cur->next->next;
        }
        return dummynode->next;
    }
};

2、19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

思路:统计链表的长度,然后找到我们要删除哪一个元素(跳过即可),虽然我知道这是很幼稚的方法,但是我还是写了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummynode=new ListNode(0);
        dummynode->next=head;
        ListNode *cur;
        cur=dummynode;
        int ct=0;
        while(cur->next!=NULL)
        {
            cur=cur->next;
            ct++;
        }
        cur=dummynode;
        int p;
        p=ct-n;
        while(p--)
        {
        cur=cur->next;
        }
        cur->next=cur->next->next;
        return dummynode->next;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummynode=new ListNode(0);
        dummynode->next=head;
        ListNode *slow,*fast;
        fast=dummynode;
        slow=dummynode;
        n++;
        while(n--&&fast!=NULL)
        {
            fast=fast->next;
        }
        while(fast!=NULL)
        {
            fast=fast->next;
            slow=slow->next;
        }
        slow->next=slow->next->next;
        return dummynode->next;
    }
};

看了卡哥的视频,简直就是绝妙!通过利用双指针,一个快指针,一个慢指针,快指针先走n步,再快慢指针同时走,等到快指针走到null时,慢指针走了count-n,就是

面试题 02.07. 链表相交 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lena=0,lenb=0;
        ListNode *cura,*curb;
        cura=headA;
        curb=headB;
        while(cura!=NULL)
        {
            cura=cura->next;
            lena++;
        }
        cura=headA;
         while(curb!=NULL)
        {
            curb=curb->next;
            lenb++;
        }
        curb=headB;
        if(lena<lenb)
        {
            swap(lena,lenb);
            swap(cura,curb);
        }
        int gap=lena-lenb;
        while(gap--)
        {
            cura=cura->next;
        }
        while(cura!=NULL)
        {
            if(cura==curb)
            return cura;
            cura=cura->next;
            curb=curb->next;
        }
        return NULL;
    }
};

142. 环形链表 II - 力扣(LeetCode)

把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II_哔哩哔哩_bilibili无思路

以下是学习的快慢指针法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast,*slow;
        fast=head;
        slow=head;
        while(fast!=NULL&&fast->next!=NULL)
        {
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow)
            {
                ListNode *index1=fast;
                ListNode *index2=head;
                while(index1!=index2)
                {
                    index2=index2->next;
                    index1=index1->next;
                }
                return index2;
            }
        }
        return NULL;
    }
};

在博客中看到了另一位uu的解法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值