leetcode19删除链表倒数第N个节点(记录)C++

该文展示了如何使用C++编程语言来实现删除链表中倒数第n个节点的功能。两种方法被提出:一般方法是通过计算链表长度和遍历来找到目标节点,而双指针方法则更高效,通过两个指针,一个快指针先移动n步,然后两个指针同步移动直到快指针到达链表尾部,此时慢指针指向的就是要删除的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 一般方法:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    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) {}
    };
    
    void printflistnode(ListNode *head);
    
    template<class T>
    ListNode* constructListnode(vector<T> vec);
    
    class Solution
    {
    public:
        ListNode *removeNthFromEnd(ListNode *head, int n)
        {
            if (n <= 0)
                return head;
            // 计算链表长度
            int len = 0;
            ListNode *anode = new ListNode();
            anode = head;
            while (anode != nullptr)
            {
                len++;
                anode = anode->next;
            }
    
            // 查找并删除指定元素
            if (n > len)
                return head;
            else if (n == len)
            {
                head = head->next;
                printflistnode(head);
                return head;
            }
            else
            {
                int reverse_idx = len;
                anode = head;
                while (reverse_idx != n + 1)
                {
                    anode = anode->next;
                    reverse_idx--;
                }
                anode->next = anode->next->next;
            }
            printflistnode(head);
            return head;
        }
    };
    
    int main()
    {
        // 构建链表
        vector<int> vec = {1,2,3,4,5};
        ListNode *head=constructListnode(vec);
    
        // printf链表
        printflistnode(head);
    
        cout << endl;
    
        Solution a;
        a.removeNthFromEnd(head, 1);
    
        // printflistnode(head);
    
        delete[] head;
    
        return 0;
    }
    
    void printflistnode(ListNode *head)
    {
        ListNode *next_node = head;
        while (next_node != nullptr)
        {
            cout << next_node->val;
            next_node = next_node->next;
        }
    }
    
    template<class T>
    ListNode* constructListnode(vector<T> vec)
    {
        ListNode* head=nullptr;
        if (vec.size() > 0)
        {
            head = new ListNode(vec[0]);
            ListNode *last_node = new ListNode();
    
            for (int i = 1; i < vec.size(); i++)
            {
                if (i == 1)
                {
                    head->next = new ListNode(vec[i]);
                    last_node = head->next;
                }
                else
                {
                    last_node->next = new ListNode(vec[i]);
                    last_node = last_node->next;
                }
            }
        }
        return head;
    }

  2. 双指针方法:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    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) {}
    };
    
    int printflistnode(ListNode *head);
    
    template<class T>
    ListNode* constructListnode(vector<T> vec);
    
    class Solution
    {
    public:
        ListNode *removeNthFromEnd(ListNode *head, int n)
        {
            if(head==nullptr) return head;
            ListNode *fast_ptr=new ListNode();
            fast_ptr=head;
            ListNode *slow_ptr=new ListNode();
            slow_ptr=head;
            int idx=n;
            while(idx--)
            {
                fast_ptr=fast_ptr->next;
                if(fast_ptr==nullptr && idx!=0)
                    return head;
            }
            ListNode *last_prt=new ListNode();
            last_prt=head;
            while(fast_ptr!=nullptr)
            {
                fast_ptr=fast_ptr->next;
                last_prt=slow_ptr;
                slow_ptr=slow_ptr->next;
            }
            if(slow_ptr==head)
                head=head->next;
            else
                last_prt->next=slow_ptr->next;
            printflistnode(head);
            return head;
        }
    };
    
    int main()
    {
        // 构建链表
        vector<int> vec = {1,2,3,4,5,6};
        ListNode *head=constructListnode(vec);
    
        // printf链表
        printflistnode(head);
    
        cout << endl;
    
        Solution a;
        a.removeNthFromEnd(head, 1);
    
        // printflistnode(head);
    
        delete[] head;
    
        return 0;
    }
    
    int printflistnode(ListNode *head)
    {
        ListNode *next_node = head;
        while (next_node != nullptr)
        {
            cout << next_node->val;
            next_node = next_node->next;
        }
        return 0;
    }
    
    template<class T>
    ListNode* constructListnode(vector<T> vec)
    {
        ListNode* head=nullptr;
        if (vec.size() > 0)
        {
            head = new ListNode(vec[0]);
            ListNode *last_node = new ListNode();
    
            for (int i = 1; i < vec.size(); i++)
            {
                if (i == 1)
                {
                    head->next = new ListNode(vec[i]);
                    last_node = head->next;
                }
                else
                {
                    last_node->next = new ListNode(vec[i]);
                    last_node = last_node->next;
                }
            }
        }
        return head;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值