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

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

  • 203.移除链表元素
  • 707.设计链表
  • 206.反转链表

203.移除链表元素

文章讲解

/**
 * 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* removeElements(ListNode* head, int val) {
        ListNode* tmp = new ListNode();
        ListNode* dummyHead = new ListNode(0, head);
        ListNode* p = dummyHead;
        while (p->next != nullptr) {
            if (p->next->val == val) {
                tmp = p->next;
                p->next = tmp->next;
                delete tmp;
            } else {
                p = p->next;
            }
        }
        return dummyHead->next;
    }
};

707.设计链表

文章讲解

class MyLinkedList {
public:
    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) {}
    };
    
    MyLinkedList() {
        _dummyHead = new ListNode();
        _size = 0;
    }
    
    int get(int index) {
        if (index >= _size || index < 0) 
            return -1;
        ListNode* p = _dummyHead->next;
        while (index--)
            p = p->next;
        return p->val;
    }
    
    void addAtHead(int val) {
        ListNode* node = new ListNode(val, _dummyHead->next);
        _dummyHead->next = node;
        _size++;
    }
    
    void addAtTail(int val) {
        ListNode* node = new ListNode(val);
        ListNode* p = _dummyHead;
        while (p->next != nullptr) 
            p = p->next;
        p->next = node;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > _size) 
            return;
        if (index == _size) {
            addAtTail(val);
            return;
        }
        ListNode* p = _dummyHead;
        while (index--) 
            p = p->next;
        ListNode* node = new ListNode(val, p->next);
        p->next = node;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= _size || index < 0) 
            return;
        ListNode* p = _dummyHead;
        ListNode* t = new ListNode();
        while (index--)
            p = p->next;
        t = p->next;
        p->next = t->next;
        delete t;
        t = nullptr;
        _size--;
    }
private:
    int _size;
    ListNode* _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.反转链表

文章讲解

/**
 * 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* reverseList(ListNode* head) {
        if (head == nullptr)
            return head;
        ListNode* dummyHead = new ListNode(0, head);
        ListNode* p = head->next;
        ListNode* tmp;
        while (p != nullptr) { // 头插法
            tmp = p;
            p = p->next;
            tmp->next = dummyHead->next;
            dummyHead->next = tmp;
        }
        head->next = nullptr;
        return dummyHead->next;
    }
};

精进可用双指针法

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