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

203.移除链表元素

首次尝试:成功

链表比较基础的按值查找并删除,分带/不带虚拟头结点。带虚拟头节点就不用单独讨论删除头结点情况。

语言: cpp


/**
 * 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* dummyhead=new ListNode(0);//new别忘了!
        dummyhead->next=head;
        ListNode* cur=dummyhead;
        while(cur->next){ //cur要用来指向下下一个!
            if(cur->next->val==val){
                ListNode* temp=cur->next;
                cur->next=cur->next->next;
                delete temp;
            }else {
                cur = cur->next;
            }//else别忘了!

        }
        return dummyhead->next;//用三行把虚头节点删了也行
        


    }
};

707.设计链表!!

本体逻辑不难,细节要注意。

首次尝试:代码逻辑基本正确,代码有些错误。1.要在类里定义变量size和dummyhead虚结点来初始化链表。2.不能把size--当作while循环里的条件,不同于index。3.有的记得加上size++或size--。

其他注意点:1.可以通过函数调用来简化该代码。2.必要时可以自己写下结点的结构体定义。3。区分链表的初始化与结点的定义。

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

    
    MyLinkedList() {
        dummyhead=new ListNode(0);
        size=0;

    }
    
    
    int get(int index) {
        if(index<0||index>=size){
            return -1;
        }else{
            ListNode* cur=dummyhead->next;
            while(index--){
                cur=cur->next;
            }
            return cur->val;
        }

    }
    
    void addAtHead(int val) {
        ListNode* newhead=new ListNode(val);
        newhead->next=dummyhead->next;
        dummyhead->next=newhead;
        size++;

    }
    
    void addAtTail(int val) {
        ListNode* cur=dummyhead;

        while(cur->next != nullptr){//不能写成size--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            cur=cur->next;
        }
        ListNode* newtail=new ListNode(val);
        cur->next=newtail;
        size++;//!
    }
    
    void addAtIndex(int index, int val) {
        ListNode* newNode=new ListNode(val);
        ListNode* cur=dummyhead;
        if(index>size||index<0){
            return;
        }else{
            while(index--){
                cur=cur->next;
            }
            newNode->next=cur->next;
            cur->next=newNode;
            }
        size++;

    }
    
    void deleteAtIndex(int index) {
        ListNode* cur=dummyhead;
        if(index>=size||index<0){
            return;
        }else{
            while(index--){
                cur=cur->next;
            }
            ListNode* temp=cur->next;
            cur->next=cur->next->next;
            delete temp;
        }
        size--;//!!!!!!
    }

private:
    int size;//引入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.反转链表!!

首次尝试:成功。用的是双指针法。本题递归法特别是法三的代码理解有点难。

法一:双指针 法二:前向递归 法三:反向递归(正向反向由先递归还是先执行代码来控制)

本题3种方法可直接参考代码随想录。

代码随想录 (programmercarl.com)icon-default.png?t=N7T8https://www.programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值