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

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 nummpyhead(0,head);
        ListNode* first=&nummpyhead;
        if(head==NULL) return head;
        ListNode* second;
        while(first->next!=NULL&&first->next->next!=NULL){
            second=first->next;
            ListNode* tmp=second->next;
            first->next=tmp;
            second->next=tmp->next;
            tmp->next=second;
            first=first->next->next;
            
        }
        return nummpyhead.next;

    }
};

要注意的是除了交换的两个元素外,他们前一个的元素指针也需要改变,所以在链表头部没有前一个结点,那就和其他地方的操作不同,故而使用虚拟头结点。

19. 删除链表的倒数第 N 个结点

先使用了暴力算法,遍历链表元素个数后for循环找到结点删除

/**
 * 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 nummpyhead(0,head);
        ListNode* cur=&nummpyhead;
        int num=0;
        while(cur->next!=NULL){
            num++;
            cur=cur->next;
        }
        cur=&nummpyhead;
        for(int i=0;i<num-n;i++){
            cur=cur->next;
        }
        ListNode* tmp=cur->next;
        cur->next=tmp->next;
        delete tmp;
        return nummpyhead.next;
    }
};

正确做法应该使用双指针 ,用到了删除所以依旧使用虚拟头结点,倒数n个那就让快指针比慢指针高出n个,当快指针到最后一个结点时,所以是慢指针的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 nummpyhead(0,head);
        ListNode* slow=&nummpyhead;
        ListNode* fast=slow;
        for(int i=0;i<n;i++){
            fast=fast->next;
        }
        while(fast->next!=NULL){
            fast=fast->next;
            slow=slow->next;
        }
        ListNode* tmp=slow->next;
        slow->next=tmp->next;
        delete tmp;
        return nummpyhead.next;
    }
};

 面试题 02.07. 链表相交

先是猪脑不理解链表相交的双指针的暴力解法:

/**
 * 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) {
        ListNode* cura=headA;
        ListNode* curb=headB;
        while(cura!=NULL){
            while(curb!=NULL){
                if(cura==curb)return curb;
                curb=curb->next;
            }
            cura=cura->next;
            curb=headB;
        }
        return NULL;
    }
};

链表相交是指向的元素完全一样,地址一样,是那个结点完全一样。所以可以双指针快慢,快的先去把长的链表找到和短的链表一样长的部分,再一个个对比:

/**
 * 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) {
        ListNode* cura=headA;
        ListNode* curb=headB;
        int lenA=0;
        int lenB=0;
        while(cura!=NULL){
            lenA++;
            cura=cura->next;
        }
        while(curb!=NULL){
            lenB++;
            curb=curb->next;
        }
        if(lenB>lenA){
            swap(lenA,lenB);
            swap(headA,headB);
        }
        cura=headA;
        curb=headB;
        for(int i=0;i<lenA-lenB;i++){
            cura=cura->next;
        }
        while(cura!=NULL){
            if(cura==curb)return cura;
            cura=cura->next;
            curb=curb->next;
        }
        return NULL;


    }
};

142. 环形链表 II 

 先是写出了暴力解,一个指针在前面走,另个在后面遍历,有没有两个指针next相等,那就是指向环的入口了。

 

/**
 * 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) {
        int pos=-1;
        
        ListNode nummpyhead(0,head);
        ListNode* cur1=&nummpyhead;
        ListNode* cur2=cur1->next;
        while(cur2!=NULL){
            int i=0;
            cur1=&nummpyhead;
            while(cur1!=cur2){
                if(cur2->next==cur1->next){
                    pos=i;
                    return cur1->next;
                }
                i++;
                cur1=cur1->next;
            }
            cur2=cur2->next;


            }

        return NULL;

        }

    
};

然后是正确做法,非常需要数学思维,不看讲解不懂的 

使用双指针,一个一次走两步,一个一次一步,等相遇就是有环,相遇后再走至路口的距离和开头走到入口的距离一样。

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

    
};

具体的数学证明在代码随想录代码随想录 (programmercarl.com) 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值