算法训练营笔记day03|203. 移除链表元素、707. 设计链表、206. 反转链表

203. 移除链表元素

题目连接

笔记

while (pre != NULL && pre->next != NULL)pre != NULL要放在pre->next != NULL前面。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL) {
            return NULL;
        }
        while (head != NULL && head->val == val) {
            ListNode *temNode = head;
            head = head->next;
            delete temNode;
        }
        ListNode *pre = head;
        while (pre != NULL && pre->next != NULL) {
            if (pre->next->val == val) {
                pre->next = pre->next->next;
            }
            else {
                pre = pre->next;
            }
        }
        return head;
    }
};

707. 设计链表

题目连接

笔记

可以在类内定义链表节点,记得写构造函数,边界条件的判断要细心。

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode *next;
        LinkedNode(int val) : val(val), next(nullptr) {};
    };
private:
    LinkedNode *_dummyhead;
    int size;
public:
    MyLinkedList() {
        _dummyhead = new LinkedNode(0);
        size = 0;
    }
    
    int get(int index) {
        if (index >= size || index < 0) {
            return -1;
        }
        LinkedNode *cur = _dummyhead->next;
        for (int i = 0; i < index; i++) {
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode *newNode = new LinkedNode(val);
        newNode->next = _dummyhead->next;
        _dummyhead->next = newNode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode *newNode = new LinkedNode(val);
        LinkedNode *tem = _dummyhead;
        while (tem->next != nullptr) {
            tem = tem->next;
        }
        tem->next = newNode;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > size) {
            return;
        }
        else if (index <= 0) {
            addAtHead(val);
        }
        else {
            LinkedNode *tem = _dummyhead;
            LinkedNode *newNode = new LinkedNode(val);
            for (int i = 0; i < index; i++) {
                tem = tem->next;
            }
            newNode->next = tem->next;
            tem->next = newNode;
            size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if (index >= size || index < 0) {
            return;
        }
        LinkedNode *tem = _dummyhead;
        while (index--) {
            tem = tem->next;
        }
        LinkedNode *delNode = tem->next;
        tem->next = tem->next->next;
        delete delNode;
        size--;
    }
};

206. 反转链表

题目连接

笔记

遍历链表并将各个节点的指针反转,while里面是cur而不是cur->next

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = nullptr;
        ListNode *cur = head;
        while (cur) {
            ListNode *tem = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tem;
        }
        return pre;
    }
};

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