代码随想录 | Day4:leetcode 22.两两交换链表中的结点 19.删除链表的倒数第n个结点 面试02.07链表相交 142.环形链表

22两两交换链表中的结点

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyHead = new ListNode();
        dummyHead->next = head;
        ListNode* cur = dummyHead;

        while(cur->next!=nullptr&&cur->next->next!=nullptr){
            ListNode* temp = cur->next;
            // temp1 = cur->next->next;
            ListNode* temp2 = cur->next->next->next;
            cur->next = cur->next->next;
            cur->next->next = temp;
            temp->next = temp2;
            cur = temp;
        }
        return dummyHead->next;
    }
};

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

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyHead = new ListNode();
        dummyHead->next = head;
        ListNode* fast = dummyHead;
        ListNode* slow = dummyHead;
        
        while(n && fast->next != nullptr){
            fast = fast->next;
            n--;
        };
        while(fast->next != nullptr){
            fast = fast->next;
            slow = slow->next;
        };
        ListNode* temp = slow->next;
        slow->next = slow->next->next;
        delete temp;
        return dummyHead->next;
    }
};

面试02.07链表相交

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int sizeA = 0;
        int sizeB = 0;
        ListNode* curA = headA;
        ListNode* curA1 = headA;
        ListNode* curB = headB;
        ListNode* curB1 = headB;
        while(curA != nullptr){
            curA = curA->next;
            sizeA++;
        };
        while(curB != nullptr){
            curB = curB->next;
            sizeB++;
        };
        if(sizeA>=sizeB){
            int a = sizeA - sizeB;
            while(a--){
                curA1 = curA1->next;
            };
            while(sizeB--){
                if(curA1 == curB1){
                    return curA1;
                };
                curA1 = curA1->next;
                curB1 = curB1->next;
            };
            return nullptr;
        }else{
            int b = sizeB - sizeA;
            while(b--){
                curB1 = curB1->next;
            };
            while(sizeB--){
                if(curA1 == curB1){
                    return curA1;
                };
                curA1 = curA1->next;
                curB1 = curB1->next;
            };
            return nullptr;
        }
    }
};

142.环形链表(数学推导还是比较难想到的,代码实现不复杂,见代码随想录)

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 index1;
            };
        };
        return NULL;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值