代码随想录算法训练营第3天| 203.移除链表元素 707.设计链表 206.反转链表

文章讨论了如何在链表中删除指定元素,包括使用虚拟头节点的方法。另外,还介绍了MyLinkedList类的设计,支持在链表头部、尾部和指定位置插入和删除元素。最后,提到了链表的反转问题,提供了一个解决方案。
摘要由CSDN通过智能技术生成
  • 今日学习的文章链接,或者视频链接

第二章 链表part01

  • 自己看到题目的第一想法

  • 看完代码随想录之后的想法

203

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
        dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
        ListNode* prev = dummyHead;
        ListNode* cur = head;

        while (cur != nullptr){
            if (cur->val == val){
                prev->next = cur->next;
                ListNode* tmp = cur;
                cur = cur->next;
                delete tmp;
            }else{
                prev = cur;
                cur = cur->next;
            }
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

707:

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr){}
    };
    MyLinkedList() {
        _dummyhead = new LinkedNode(0);
        _size = 0;
    }
    
    int get(int index) {
        if (index < 0 || index >= _size){
            return -1;
        }
        auto cur = _dummyhead->next;
        while (index){
            cur = cur->next;
            index--;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        auto newNode = new LinkedNode(val);
        newNode -> next = _dummyhead-> next;
        _dummyhead -> next = newNode;
        _size++;
    }
    
    void addAtTail(int val) {
        auto newNode = new LinkedNode(val);
        auto cur = _dummyhead;
        while(cur->next!=nullptr){
            cur = cur->next;
        }
        cur->next = newNode;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > _size||index<0) return;

        auto newNode = new LinkedNode(val);
        auto cur = _dummyhead;
        while(index){
            cur = cur->next;
            index--;
        }
        newNode -> next = cur-> next;
        cur -> next = newNode;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        if (index < 0 || index >= _size){
            cout<<"error";
            return;
        }
        auto cur = _dummyhead;
        while(index){
            cur = cur->next;
            index--;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        _size--;
    }
private:
    int _size;
    LinkedNode* _dummyhead;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

206:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        auto cur = head;
        while(cur!=nullptr){
            auto tmp = cur->next;
            cur->next = prev;
            prev = cur;
            cur = tmp;
        }
        return prev;

        /*
        if (head == NULL || head->next == NULL) {
            return head;
        }
        ListNode* last = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return last;
        */
    }
};

  • 自己实现过程中遇到哪些困难

  • 今日收获,记录一下自己的学习时长

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值