代码随想录 - 训练营8期- day 3

文章讲述了在LeetCode中关于链表操作的三个问题:如何使用虚拟头结点简化移除链表元素的过程,设计一个自定义链表类,以及如何反转链表。在移除元素时引入了虚拟头结点的概念,使操作更简便。在设计链表时,包含了添加、获取、删除元素等基本功能。最后提到了反转链表的常见方法。
摘要由CSDN通过智能技术生成
203. 移除链表元素
707. 设计链表
206. 反转链表

203. 移除链表元素

因为在原来的链表上操作要考虑头结点感觉有点麻烦,

于是学习了一下“虚拟头结点”

总而言之就是,先创建个虚拟头结点,让其 next 指向head,

于是就可以批量操作了,一个循环直接解决

至于 delete 的问题,leetcode 不 delete 也是能过的,但是都写 cpp 了那 delete 肯定要养成好习惯

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyhead = new ListNode();
        dummyhead->next = head;
        ListNode* cur = dummyhead;
        while(head != nullptr)
        {
            if(head->val == val)
            {
                cur->next = head->next;
                delete head;
                head = cur->next;
            }
            else
            {
                cur = cur->next;
                head = head->next;
            }
        }
        head = dummyhead->next;
        delete dummyhead;
        return head;
    }
};

707. 设计链表

这个题还是挺综合的……

写时候有点困,导致犯了个低级错误找了十年……

注意 addAtIndex 函数,index > size 时直接返回......

class MyLinkedList {
private:
    struct Node {
        int val;
        Node* next;
        Node() : val(0), next(nullptr) {}
        Node(int _val) : val(_val), next(nullptr) {}
    };

    Node* dummyhead;
    int _size = 0;
public:
    MyLinkedList() {
       dummyhead = new Node(-1);
    }
    
    int get(int index) {
        Node* cur = dummyhead->next;
        for(int i = 0; i < _size; ++i) {
            if(i == index) {
                break;
            }
            cur = cur->next;
        }

        return (index < 0 || index >= _size) ? -1 : cur->val;
    }
    
    void addAtHead(int val) {
        Node* head = new Node(val);
        head->next = dummyhead->next;
        dummyhead->next = head;
        _size++;
    }
    
    void addAtTail(int val) {
        Node* cur = dummyhead;
        while(cur->next != nullptr) {
            cur = cur->next;
        }
        Node* tail = new Node(val);
        cur->next = tail;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > 0 && index < _size) {
            Node* node = new Node(val);
            Node* cur = dummyhead->next;
            for(int i = 0; i < _size - 1; ++i) {
                if(i + 1 == index) {
                    node->next = cur->next;
                    cur->next = node;
                }
                cur = cur->next;
            }
            _size++;
        }
        if(index <= 0) {
            addAtHead(val);
        }
        if(index == _size) {
            addAtTail(val);
        }
    }
    
    void deleteAtIndex(int index) {
        if(index < 0 || index >= _size) {
            return;
        }

        Node* cur = dummyhead;
        for(int i = 0; i < _size; ++i) {
            if(i == index) {
                Node* tmp = cur->next;
                cur->next = tmp->next;
                delete tmp;
            }
            cur = cur->next;
        }
        _size--;
    }
};

206. 反转链表

这个水,但凡上过数据结构应该都会吧。

明天补一下递归吧……

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* cur = head;
        while(cur != nullptr)
        {
            ListNode* next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        return prev == nullptr ? head : prev;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值