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

24.两两交换链表中的节点(参考答案)

/**
 * 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(0);
        dummyNode->next=head;
        ListNode* cur =dummyNode;
        while(cur->next!=nullptr&&cur->next->next!=nullptr)
        {
            ListNode* tmp=cur->next;
            ListNode* tmp1=cur->next->next->next;  //记录的临时变量
            //交换的三个步骤
            cur->next=cur->next->next;
            cur->next->next=tmp;
            tmp->next=tmp1;

            cur=cur->next->next;
        }
        return dummyNode->next;
    }
};

需要理解为什么 记录的临时变量为 cur->next  cur->next->next->next ,改变指向后无法获取到的节点需要临时记录下来,以及交换的三个步骤

19.删除链表倒数第n个节点

/**
 * 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* fast=dummyNode;
        ListNode* slow=dummyNode;
        while(n--&&fast!=nullptr)
        {
            fast=fast->next;
        }
        fast=fast->next;
        while(fast!=nullptr)
        {
            fast=fast->next;
            slow=slow->next;
        }
        slow->next=slow->next->next;

        return dummyNode->next;
    }
};

双指针法,问题不大,只要能想起来,思维还是有点跟不上

02.07. 链表相交(参考答案)

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;
        int lenA = 0, lenB = 0;
        while (curA != NULL) { // 求链表A的长度
            lenA++;
            curA = curA->next;
        }
        while (curB != NULL) { // 求链表B的长度
            lenB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头,lenA为其长度
        if (lenB > lenA) {
            swap (lenA, lenB);
            swap (curA, curB);
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while (gap--) {
            curA = curA->next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != NULL) {
            if (curA == curB) {
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;
    }
};

虽然是简单体型,但有一些小细节需要注意,求长度,使a为长链表,移动指针和b指向同一位置,这些都要考虑到

142. 环形链表

/**
 * 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=head;
        ListNode* slow=head;
        while(fast!=nullptr&&fast->next!=nullptr)
        {
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow)
            {
                ListNode* index1=fast;
                ListNode* index2=head;
                while(index1!=index2)
                {
                    index1=index1->next;
                    index2=index2->next;
                }
                return index2;
            }
        }
        return nullptr;
    }
};

可以分为两块,1.先找是否有环 2.环的入口

需要明白其原理 fast和slow肯定是在环内相遇,但是如何找到入口需要对公式有一定的理解

目前我还是无法独立完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值