代码随想录day03

203

链表基础操作

cpp 复制代码
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while (head!=NULL&&head->val==val)
        {
            ListNode* temp=head;
            head=head->next;
            delete temp;
        }
        ListNode* cur=head;
        while (cur!=NULL&&cur->next!=NULL)
        {
            if(cur->next->val==val){
                ListNode* temp=cur->next;
                cur->next=cur->next->next;
                delete temp;
            }else{
                cur=cur->next;
            }
        }
        return head;
        
    }
};

707

多写多试

cpp 复制代码
class MyLinkedList {
public:
    struct LinkedNode
    {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val),next(nullptr){}
    };
    MyLinkedList() {
        head=new LinkedNode(0);
        size=0;
    }
    
    int get(int index) {
        if(index>(size-1)||index<0){
            return -1;
        }
        LinkedNode* cur=head->next;
        while (index--)
        {
            cur=cur->next;
        }
        return cur->val;        
    }
    
    void addAtHead(int val) {
        LinkedNode*newNode=new LinkedNode(val);
        newNode->next=head->next;
        head->next=newNode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode*newNode=new LinkedNode(val);
        LinkedNode* cur=head;
        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;
        LinkedNode*newNode=new LinkedNode(val);
        LinkedNode* cur=head;
        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=head;
        while(index--){
            cur=cur->next;
        }
        LinkedNode* temp = cur->next;
        cur->next = cur->next->next;
        delete temp;
        temp=nullptr;
        size--;
    }

private:
    int size;
    LinkedNode* head;

};

206

利用双指针法,画图理解

cpp 复制代码
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* temp; 
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur) {
            temp = cur->next;  
            cur->next = pre; 
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
相关推荐
2401_8588698011 分钟前
支持向量机
算法·机器学习·支持向量机
用户48221371677528 分钟前
深度学习——卷积神经网络
算法
DashingGuy31 分钟前
算法(keep learning)
java·数据结构·算法
兔兔西33 分钟前
【数据结构、java学习】数组(Array)
java·数据结构·算法
007php00734 分钟前
Go语言面试:传值与传引用的区别及选择指南
java·开发语言·后端·算法·面试·golang·xcode
小徐不徐说34 分钟前
数据结构基础之队列:数组/链表
c语言·数据结构·算法·链表·面试
fantasy_arch1 小时前
SVT-AV1 svt_aom_motion_estimation_kernel 函数分析
人工智能·算法·av1
星逝*2 小时前
LeetCode刷题-top100( 矩阵置零)
算法·leetcode·矩阵
zandy10112 小时前
LLM与数据工程的融合:衡石Data Agent的语义层与Agent框架设计
大数据·人工智能·算法·ai·智能体
max5006003 小时前
本地部署开源数据生成器项目实战指南
开发语言·人工智能·python·深度学习·算法·开源