代码随想录03| 203.移除链表元素,707.设计链表, 206.反转链表

链表基础:文章链接

203. 移除链表元素

题目链接:203.移除链表元素
文档讲解:文档讲解链接
视频讲解:视频讲解链接

代码思路:单向链表移除元素,将当前节点的上一个节点的下一个节点连接上当前节点的下一个节点,注意移除头结点有些特殊,可以设置虚拟节点来省去单独处理头节点的过程。

/**
 * 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* dummy_head = new ListNode(0);
        dummy_head->next = head;
        ListNode* cur = dummy_head;
        //这里必须用while,循环判断当前节点的下一个节点是否也满足条件,不能用for
        while (cur->next != nullptr) {
            if (cur->next->val == val) {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            } else {
                cur = cur->next;
            }
        }
        head = dummy_head->next;
        delete dummy_head;
        return head;
    }
};

707.设计链表

题目链接:707.设计链表
文档讲解:文档讲解链接
视频讲解:视频讲解链接

解题思路:使用结构题定义链表的基本属性,按照题目要求实现链表的功能。

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val),next(nullptr){}

    };

    MyLinkedList() {
        __dummyHead = new LinkedNode(0);
        __size = 0;
    }
    
    int get(int index) {
        if (index < 0 || index >= __size){
            return -1;
        }
        LinkedNode* cur = __dummyHead;
        for(int i = 0; i < index; i++) {
            cur = cur->next;
        }
        return cur->next->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* tempNode = new LinkedNode(val);
        tempNode->next = __dummyHead->next;
        __dummyHead->next = tempNode;
        __size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* cur = __dummyHead;
        while (cur->next != nullptr) {
            cur = cur->next;
        }
        LinkedNode* tempNode = new LinkedNode(val);
        cur->next = tempNode;
        __size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > __size) return;
        if (index < 0) index = 0;
        LinkedNode* cur = __dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur->next;
        }
        LinkedNode* tempNode = new LinkedNode(val);
        tempNode->next = cur->next;
        cur->next = tempNode;
        __size++;

    }
    
    void deleteAtIndex(int index) {
        if (index >= __size || index < 0) {
            return;
        }//注意这里需要判断无返回值
        LinkedNode* cur = __dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur->next;
        }
        LinkedNode* tempNode = cur->next;
        cur->next = cur->next->next;
        delete tempNode;
        tempNode = nullptr;
        __size--;

    }
private:
    int __size;
    LinkedNode* __dummyHead;

};

/**
 * 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.反转链表

题目链接:206.反转链表
文档讲解:文档讲解链接
视频讲解:视频讲解链接

代码思路:通过递归将链表的指针反向。

/**
 * 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* reverseRecursion(ListNode* pre, ListNode* cur) {
        if (cur == nullptr) {//递归的结束条件
            return pre;
        }
        ListNode* temp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = temp;
        // delete temp;
        return reverseRecursion(pre, cur);
    }

    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        return reverseRecursion(pre, cur);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值