Day4 | LeetCode 24. 两两交换链表中的节点 19. 删除链表的倒数第 N 个结点 面试题 02.07. 链表相交 142. 环形链表 II

本文介绍了LeetCode中关于链表操作的四个经典问题:两两交换链表节点、删除链表倒数第N个节点、寻找链表相交点以及检测环形链表。通过双指针技巧实现高效解法。
摘要由CSDN通过智能技术生成

LeetCode 24. 两两交换链表中的节点

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr) {
            return head;
        }
        ListNode* dummyHead = new ListNode();
        dummyHead->next = head;
        ListNode* fast = head;
        ListNode* slow = dummyHead;
        while (fast != nullptr && fast->next != nullptr) {
            ListNode* temp = fast->next->next;
            fast->next->next = slow->next;
            slow->next = fast->next;
            fast->next = temp;
            slow = fast;
            fast = fast->next;
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

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

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

LeetCode 面试题 02.07. 链表相交

利用差值求相交节点
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int n = 0;
        ListNode* curA = headA;
        ListNode* curB = headB;
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }
        while (curA->next != nullptr) {
            n++;
            curA = curA->next;
        }
        while (curB->next != nullptr) {
            n--;
            curB = curB->next;
        }
        if (curA != curB) {
            return nullptr;
        }
        curA = n >= 0 ? headA : headB;
        curB = curA == headA ? headB : headA;
        n = abs(n);
        while (n--) {
            curA = curA->next;
        }
        while (curA != curB) {
            curA = curA->next;
            curB = curB->next;
        }
        return curA;
    }
};
双指针
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }
        ListNode* curA = headA;
        ListNode* curB = headB;
        while (curA != curB) {
            curA = curA == nullptr ? headB : curA->next;
            curB = curB == nullptr ? headA : curB->next;
        }
        return curA;
    }
};

LeetCode 142. 环形链表 II

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head == nullptr || head->next == nullptr || head->next->next == nullptr) {
            return nullptr;
        }
        ListNode* fast = head;
        ListNode* slow = head;
        do {
            slow = slow->next;
            fast = fast->next->next;
        }while(fast != slow && fast->next != nullptr && fast->next->next != nullptr);
        if (fast != slow) {
            return nullptr;
        }
        fast = head;
        while (fast != slow) {
            fast = fast->next;
            slow = slow->next;
        } 
        return slow;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值