链表01

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* dummy_head = new ListNode();
        dummy_head-> next = head;
        ListNode* cur = dummy_head;
        while(cur -> next != nullptr)
        {
            if(cur->next-> val == val)
            {
                ListNode* tmp = cur->next;
                cur-> next = cur ->  next -> next;
                delete  tmp;
            }
            else
            {
                cur = cur -> next;
            }
        }
        return dummy_head -> next;
    }
};

707. 设计链表

class MyLinkedList {
public:
    struct Node{
        int val;
        Node * next;
        Node(int x):val(x), next(nullptr){}
    };

    MyLinkedList() {
        _dummyHead = new Node(0); 
        _size = 0;
    }
    
    int get(int index) {
        if (index > (_size - 1) || index < 0) {
            return -1;
        }
        Node* cur = _dummyHead->next;
        while(index--){ // 如果--index 就会陷入死循环
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        Node * cur = new Node(val);
        cur -> next = _dummyHead->next;
        _dummyHead->next = cur;
        _size++;
    }
    
    void addAtTail(int val) {
        Node* newNode = new Node(val);
        Node* cur = _dummyHead;
        while(cur->next != nullptr){
            cur = cur->next;
        }
        cur->next = newNode;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > _size) return;
        if(index < 0) index = 0;        
        Node* newNode = new Node(val);
        Node* 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;
        }
        Node* cur = _dummyHead;
        while(index--) {
            cur = cur ->next;
        }
        Node* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
    
        tmp=nullptr;
        _size--;
        
    }
private:
    int _size;
    Node* _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 *cur = head;
        ListNode *pre = nullptr;
        ListNode* tmp;
        while(cur)
        {
            tmp = cur -> next;
            cur -> next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
        
    }
};
posted @ 2025-07-09 10:56  skyler886  阅读(7)  评论(0)    收藏  举报