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

纯属个人观点,如有错误,请指正,谢谢。

在学链表之前要先熟悉链表(单向)的定义:

Struct ListNode{
    int val;  
    ListNode *next;
    ListNode(): val(0), next(nullptr) {}
    ListNode(int n): val(val), next(nullptr) {}
    ListNode(int n, ListNode *node): val(val), next(node) {}
}

从定义就能看出链表的核心和难点是next(或者prev)的使用,对链表的操作都是基于next来进行的,为了更好地操作链表我们最好有画图的习惯。在对链表操作时要注意从何处来,要有标记头,常常创建虚拟头节点,不能学习路易十六——丢了头,要明白之前的,现在的和过去的,另外一定要对nullptr有警惕心理。

203. 移除链表元素

203. 移除链表元素 - 力扣(LeetCode)
要注意的是边界条件,头节点和尾节点。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *dum = new ListNode();
        ListNode *prev = dum;
        ListNode *node = head;
        while(node != nullptr){
            if(node->val != val){
                prev->next = node;
                prev = prev->next;
                node = node->next;
            }else{
                node = node->next;
                prev->next = nullptr;
            }
        }
        return dum->next;
    }
};

707. 移除链表元素

707. 设计链表 - 力扣(LeetCode)

设计类是目前的弱项,要着重训练

class MyLinkedList {
public:
    MyLinkedList() {
        this->size = 0;
        this->head = new ListNode(); // 创建一个虚拟头节点
    }
    
    int get(int index) {
        int ret = 0;
        if(index >= size || index < 0) return -1;  // 大于等于size就是不包括虚拟头节点
        ListNode* node = head;
        for(int i = 0; i <= index; i++) node = node->next;
        return node->val;
    }
    
    void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    void addAtTail(int val) {
        addAtIndex(size, val);
    }
    
    void addAtIndex(int index, int val) {
        if(index > size || index < 0) return ;
        ListNode *current = head;
        ListNode *next_cur = head;
        ListNode *node = new ListNode(val);
        if(index == size){
            while(current->next != nullptr){
                current = current->next;
            }
            current->next = node;
        }else{
            for(int i = 0; i < index; i++){
                current = current->next;
            }
            next_cur = current->next;
            current->next = node;
            node->next = next_cur;
        }
        this->size++;
    }
    
    void deleteAtIndex(int index) {
        if(index >= size || index < 0) return ;
        ListNode *current = head;
        for(int i = 0; i < index; i++){
            current = current->next;
        }
        current->next = current->next->next;
        this->size--;
    }
private:
    int size;
    ListNode *head;

};

206.反转链表 

206. 反转链表 - 力扣(LeetCode)

火车掉头

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode *node = head;
        ListNode *prev = nullptr; 
        while(node != nullptr){
            ListNode *curr = node->next;
            node->next = prev;
            prev = node;
            node = curr;
        }
        return prev;
    }
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值