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

  • 今日学习的文章链接,或者视频链接

第二章 链表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;
        */
    }
};

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

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

你可能感兴趣的:(算法,链表,数据结构)