代码随想录算法训练营第三天 | 链表part01

链表节点的定义,一定要牢记

// 单链表
struct ListNode {
    int val;  // 节点上存储的元素
    ListNode *next;  // 指向下一个节点的指针
    ListNode(int x) : val(x), next(NULL) {}  // 节点的构造函数
};

这里的构造函数非必需,但能方便我们定义新的链表节点

1. 创建一个链表节点:

ListNode* head = new ListNode(5);

2. 使用默认构造函数初始化节点:

ListNode* head = new ListNode();
head->val = 5;

203.移除链表元素

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

707.设计链表

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int x):val(x), next(nullptr){}
    };
    MyLinkedList() {
        dummyHead = new LinkedNode(0);
        size = 0;
    }
    
    int get(int index) {
        if (index > (size - 1) || index < 0) {
            return -1;
        }
        LinkedNode* cur = dummyHead->next;
        while(index--){ // 如果--index 就会陷入死循环
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* tmp = new LinkedNode(val);
        tmp->next = dummyHead->next;
        dummyHead->next = tmp;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* tmp = new LinkedNode(val);
        LinkedNode* cur = dummyHead;
        while(cur->next != nullptr){
            cur = cur->next;
        }
        cur->next = tmp;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > size) return;
        if(index < 0) index = 0;        
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = dummyHead;
        while(index--) {
            cur = cur->next;
        }
        newNode->next = cur->next;
        cur->next = newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= size || index < 0) {
            return;
        }
        LinkedNode* cur = dummyHead;
        while(index--) {
            cur = cur ->next;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tmp=nullptr;
        size--;
    }
private:
    int size;
    LinkedNode* dummyHead;
};

206.反转链表

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

while循环的判断条件一开始写的是cur->next != nullptr ,因为cur = pre会导致提前终止while循环,报错。

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