Leetcode19. 删除链表的倒数第 N 个结点-hot100-代码随想录

目录

题目:

代码(首刷自解 2024年1月13日):

代码(二刷自解 2024年3月4日)

代码(三刷自解 2024年4月22日 6min)

代码(四刷自解 2024年8月9日)


题目:


代码(首刷自解 2024年1月13日):

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == nullptr) return nullptr;
        ListNode* dummyHead = new ListNode(0,head);
        ListNode* pre = dummyHead;
        ListNode* cur = dummyHead;
        int size =  0;
        int index = 0;
        // 1.计算链表大小
        while (pre->next != nullptr) {
            pre = pre->next;
            ++size;
        }
        // 2.找到对应节点
        while (index != size - n) {
            cur = cur->next;
            ++index;
        }
        index == size-1 ?   cur->next = nullptr 
                        : cur->next = cur->next->next;

        return dummyHead->next;
    }
};

        双指针,第一个指针测量链表长度,第二个指针找到对应删除节点并删除

        时间复杂度:O(n) 空间复杂度O(1)


代码(二刷自解 2024年3月4日)

        双指针

/**
 * 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) {
        if (head == nullptr) return nullptr;
        ListNode* dummyHead = new ListNode();
        dummyHead->next = head;
        ListNode* cur = dummyHead;
        ListNode* pre = dummyHead;
        while (n-- && cur->next != nullptr) {
            cur = cur->next;
        }
        while (cur->next != nullptr) {
            cur = cur->next;
            pre = pre->next;
        }
        pre->next = pre->next == nullptr ? nullptr : pre->next->next;
        return dummyHead->next;
    }
};

代码(三刷自解 2024年4月22日 6min)

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // 删除倒数第n个,即获得倒数第n + 1 个
        if (!head) return head;
        ListNode* dummy = new ListNode(0, head);
        ListNode* cur = dummy;
        ListNode* pre = dummy;
        while (n--) {
            cur = cur->next;
        }
        while (cur->next) {
            cur = cur->next;
            pre = pre->next;
        }
        ListNode* tmp = pre->next;
        pre->next = tmp->next;
        delete tmp;
        return dummy->next;
    }
};

代码(四刷自解 2024年8月9日)

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // 双指针,找到倒数第n个节点的前一个节点
        if (!head) return head;
        ListNode* dummyHead = new ListNode(0, head);
        auto fast = dummyHead;
        auto slow = dummyHead;
        n++;//要倒数第n - 1个节点
        while (n--) fast = fast->next;
        while (fast != nullptr) {
            fast = fast->next;
            slow = slow->next;
        }
        auto tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        return dummyHead->next;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值