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) {
        //头结点就是val值
        while(head != nullptr && head->val == val){
            ListNode *tmp = head; //将等于val的元素值都删除
            head = head->next;
            delete tmp;
        }
        ListNode *cur = head;
        while(cur && cur->next){
            if(cur->next->val == val){
                ListNode *tmp = cur->next;
                cur->next = cur->next->next;
                delete tmp;
            }
            else cur = cur->next;
        }
        return head;
    }
};

心得:

  1. 要考虑头结点就是val的情况,放一起讨论很容易混乱。

707. 设计链表

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

    MyLinkedList() {
        // 设置一个虚拟头结点
        dummyhead = new LinkedNode(0);
        size = 0;
    };
    
    int get(int index) {
        if(index < 0 || index >= size) return -1;
        LinkedNode *cur = dummyhead->next;
        while(index--){
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode *cur = new LinkedNode(val);
        cur->next = dummyhead->next;
        dummyhead->next = cur;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode *tmp = new LinkedNode(val);
        LinkedNode *cur = dummyhead;
        while(cur && cur->next){
            cur = cur->next;
        }
        cur->next = tmp;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > size) return; //==index证明在末尾加
        if(index < 0) index = 0;
        LinkedNode *tmp = new LinkedNode(val);
        LinkedNode *cur = dummyhead;
        while(index--){
            cur = cur->next;
        }
        tmp->next = cur->next;
        cur->next = tmp;
        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;
        size--; //这里要细心
    }
};

/**
 * 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);
 */

心得:一定要注重细节,比如:删除节点和增加节点的时候size都要考虑,再比如:添加节点和删除节点的时候要根据题意考虑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) {
        ListNode* cur = head;
        ListNode* pre = nullptr;
        while(cur){
            ListNode *tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};

心得:在写代码前我们可以在纸上先模拟一遍过程,这样思路会清晰很多。

posted on 2023-03-06 17:32  小黑哈哈  阅读(12)  评论(0编辑  收藏  举报