代码随想录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;
    }
};
相关推荐
木子.李3474 小时前
排序算法总结(C++)
c++·算法·排序算法
闪电麦坤955 小时前
数据结构:递归的种类(Types of Recursion)
数据结构·算法
Gyoku Mint6 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
纪元A梦6 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法
px不是xp6 小时前
山东大学算法设计与分析复习笔记
笔记·算法·贪心算法·动态规划·图搜索算法
枫景Maple7 小时前
LeetCode 2297. 跳跃游戏 VIII(中等)
算法·leetcode
鑫鑫向栄7 小时前
[蓝桥杯]修改数组
数据结构·c++·算法·蓝桥杯·动态规划
鑫鑫向栄7 小时前
[蓝桥杯]带分数
数据结构·c++·算法·职场和发展·蓝桥杯
小wanga8 小时前
【递归、搜索与回溯】专题三 穷举vs暴搜vs回溯vs剪枝
c++·算法·机器学习·剪枝