代码随想录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;
    }
};
相关推荐
itzixiao12 小时前
L1-067 洛希极限(10分)[java][python]
java·开发语言·算法
jinyishu_13 小时前
链表经典OJ题
c语言·数据结构·算法·链表
葫三生13 小时前
三生原理文章被AtomGit‌开源社区收录的意义探析?
人工智能·深度学习·神经网络·算法·搜索引擎·开源·transformer
AI进化营-智能译站13 小时前
ROS2 C++开发系列15-模板实现通用算法|宏定义ROS2调试开关|一次编码适配多平台
java·c++·算法·ai
刀法如飞13 小时前
Java数组去重的20种实现方式——指导AI解决不同问题的思路
java·算法·面试
良木生香13 小时前
【C++初阶】STL——Vector从入门到应用完全指南(1)
开发语言·c++·神经网络·算法·计算机视觉·自然语言处理·数据挖掘
Brilliantwxx13 小时前
【C++】String的模拟实现(代码实现与坑点讲解)
开发语言·c++·笔记·算法
憨波个13 小时前
【说话人日志】DOVER:diarization 输出融合算法
人工智能·算法·音频·语音识别·聚类
爱学习的张大13 小时前
具身智能论文问答(四):pi0
人工智能·算法
上弦月-编程13 小时前
指针编程:高效内存管理核心
java·数据结构·算法