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

本文介绍了如何在链表中移除指定值的节点,包括使用迭代和创建临时头节点的方法,以及设计和实现链表的基本操作,如反转链表和插入节点。作者强调了实现细节和条件判断的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

203. 移除链表元素

题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description/
文章讲解:https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html
视频讲解: https://www.bilibili.com/video/BV18B4y1s7R9/
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        
        
        //当首节点的值等于val时。
        while(head != NULL && head->val == val)
        {
            ListNode *p = head->next;
            delete head;
            head = p;
            
        }
        
        //这行出问题了,没加,原因是如果删除后链表为空没有考虑到。
        if(head == NULL) return NULL;

        //接下来正常判断链表里面的值
        ListNode *q = head;
        ListNode *p = head->next;
        while(p != NULL)
        {
            
            //执行删除操作
            if(p->val == val)
            {
                q->next = p->next;
                delete p;
                p = q->next;
            }
            else
            {
                p = p->next;
                q = q->next;
            }
        }
        return head;
    }
};
原来学数据结构的时候就喜欢这么写,所以按照自己的习惯写的。
下面是创建一个头节点的写法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
                ListNode *temphead = new ListNode(0);
        temphead->next = head;
        
        ListNode *q = temphead;
        ListNode *p = head;
        
        while(p != NULL)
        {
             if(p->val == val)
            {
                q->next = p->next;
                delete p;
                p = q->next;
            }
            else
            {
                p = p->next;
                q = q->next;
            }
        }
        
        if(head == NULL) return NULL;
        
        head = temphead->next;
        delete temphead;
        return head;
    }
};

707. 设计链表

题目链接:https://leetcode.cn/problems/design-linked-list/description/
文章讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html
视频讲解:https://www.bilibili.com/video/BV1FU4y1X7WD/
我觉得这道题对我的难点应该就是各种条件的判断,具体实现不难,数据结构里面讲的很详细。
class MyLinkedList {
public:
    
    // 定义链表节点结构体
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val) : val(val), next(nullptr) {}
    };

    //初始化链表,初始化一个头节点
    MyLinkedList() {

        temphead = new LinkedNode(0);
        size = 0;
        
    }

    //获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1 。
    int get(int index) {

        if (index > (size - 1) || index < 0) {
            return -1;
        }
        
        LinkedNode *p = temphead->next;
        
        while(index)
        {
            p = p->next;
            index--;
        }
        return p->val;
    }

    //将一个值为 val
    //的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
    void addAtHead(int val) {

        LinkedNode *p = new LinkedNode(val);
        p->next = temphead->next;
        temphead->next = p;
        size++;
    }

    //将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
    void addAtTail(int val) {

        LinkedNode *p = new LinkedNode(val);
        LinkedNode *q = temphead;
        
        while(q->next != nullptr)
        {
            q = q->next;
        }
        
        q->next = p;
        size++;
    }

    // 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index
    // 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index
    // 比长度更大,该节点将 不会插入 到链表中。
    void addAtIndex(int index, int val) {

        if(index > size) return;
        if(index < 0) index = 0;  

        LinkedNode *temp = new LinkedNode(val);
        
        LinkedNode *q = temphead;
        
        
        while(index--)
        {
            q = q->next;
            
        }

        //插入到q p之间
        temp->next = q->next;
        q->next = temp;
        size++;
    }

    //如果下标有效,则删除链表中下标为 index 的节点。
    void deleteAtIndex(int index) {
        
        if (index >= size || index < 0) {
            return;
        }

        LinkedNode *p = temphead->next;
        LinkedNode *q = temphead;
        
        while(index--)
        {
            q = q->next;
            p = p->next;
        }

        //删除p
        q->next = p->next;
        delete p;
        p = q->next;
        size--;
    }

    // 打印链表
    void printLinkedList() {
        LinkedNode* cur = temphead;
        while (cur->next != nullptr) {
            cout << cur->next->val << " ";
            cur = cur->next;
        }
        cout << endl;
    }

private:
    int size;             //数组长度
    LinkedNode* temphead; //头节点,最后应该释放。
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
还是要注意实现的具体细节,不能太马虎。

206.反转链表

题目链接:https://leetcode.cn/problems/reverse-linked-list/description/
文章讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html
视频讲解:https://www.bilibili.com/video/BV1nB4y1i7eL/
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        //头插法
        ListNode *temphead = new ListNode(0);
        ListNode *p = head;
        

        while(p != NULL)
        {
            ListNode *q = p->next;
            p->next = temphead->next;
            temphead->next = p;
            p = q;
            
        }
        head = temphead->next;
        return head;

    }
};
头插法还是比较简单的,递归还不太会。先留着。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值