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

24. 两两交换链表中的节点:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;
        while(cur->next!=NULL&&cur->next->next!=NULL){
            ListNode* tmp = cur->next;
            ListNode* tmp1 = cur->next->next->next;
            cur->next = cur->next->next;
            cur->next->next = tmp;
            cur->next->next->next = tmp1;

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

这个题如果画图的话就会比较清晰的梳理逻辑了;

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

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* fast = dummyHead;
        ListNode* slow = dummyHead;//注意这里是虚头结点而不是头节点
        while(n!=0&&fast!=NULL){
            fast = fast->next;
            n--;
        }
        fast = fast->next;
        while(fast!=NULL){
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dummyHead->next;
    }
};

面试题 02.07. 链表相交

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;
        int a = 0,b = 0;
        int num = 0;
        while(curA!=NULL){
            a++;
            curA = curA->next;
        }
        while(curB!=NULL){
            b++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        if(a>b){
            int c = a-b;
            while(c!=0&&curA!=NULL){
                curA = curA->next;
                c--;
            }
        }
        if(a<b){
            int c = b-a;
            while(c!=0&&curB!=NULL){
                curB = curB->next;
                c--;
            }
        }
        while(curA!=NULL&&curB!=NULL){
            if(curA != curB){
                curA = curA->next;
                curB = curB->next;
            }
            if(curA == curB){
                return curA;
            }
        }
        return NULL;
    }
};

注意是指针相等而不是结点值相等,搞了半天;

142. 环形链表 II

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        int sut = 0;
        while(fast!=NULL&&fast->next!=NULL){
            fast = fast->next->next;
            slow = slow->next;
            if(fast==slow){
                sut = 1;
                break;
            }
        }
        ListNode* index = head;
        int num = 0;
        while(index!=slow){
            index = index->next;
            num++;
            slow = slow->next;
        }
        if(sut == 0){
            return NULL;
        }
        else{
            return index;
        }
    }
};

找环比较简单,定义快慢指针即可,找到入口需要一定数学理解,即从相交点和开头同时出发一个指针,相遇时极为4入口;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值