代码随想录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;
    }
};
相关推荐
MobotStone37 分钟前
从金鱼记忆到过目不忘:Transformer 如何让AI真正理解一句话?
算法
炽烈小老头2 小时前
【每天学习一点算法 2025/12/19】二叉树的层序遍历
数据结构·学习·算法
Xの哲學2 小时前
Linux grep命令:文本搜索的艺术与科学
linux·服务器·算法·架构·边缘计算
soft20015252 小时前
MySQL Buffer Pool深度解析:LRU算法的完美与缺陷
数据库·mysql·算法
WBluuue3 小时前
AtCoder Beginner Contest 436(ABCDEF)
c++·算法
fie88893 小时前
广义 S 变换(GST)地震信号时频谱
算法
json{shen:"jing"}4 小时前
1-C语言的数据类型
c语言·c++·算法
im_AMBER4 小时前
数据结构 13 图 | 哈希表 | 树
数据结构·笔记·学习·算法·散列表
LYFlied4 小时前
【算法解题模板】动态规划:从暴力递归到优雅状态转移的进阶之路
数据结构·算法·leetcode·面试·动态规划
Hcoco_me4 小时前
RTMPose_JSON相关解读
算法·数据挖掘·json·聚类