03. 链表理论基础,203.移除链表元素, 707.设计链表, 206.反转链表

单链表节点定义:

// 单链表
struct ListNode {
    int val;  // 节点上存储的元素
    ListNode *next;  // 指向下一个节点的指针
    ListNode(int x) : val(x), next(NULL) {}  // 节点的构造函数
};

 

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        ListNode* dummyHead = new ListNode(0);//申请独立的空间创建虚拟头节点
        dummyHead->next = head; //虚拟头节点的指针域指向真正的头节点

        //移除头结点和移除其他节点的操作是不一样的
        //因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点
        //从虚拟头节点出发 开始删除元素 使得头节点的处理方式和常规节点一样
        //在处理完链表后直接删除虚拟头节点
        ListNode* cur = dummyHead; //不用为cur申请独立的空间
        while(cur->next != nullptr){
            if(cur->next->val == val){
                ListNode* temp= cur->next;
                cur->next = cur->next->next;
                delete temp; temp = nullptr;
            }
            else cur = cur->next;
        }
        head = dummyHead->next;
        delete dummyHead; dummyHead = nullptr;
        return head;
    }
};

class MyLinkedList {
public:

    struct ListNode{
        ListNode* next;
        int val;
        ListNode(int value): val(value), next(nullptr){} //注意初始化列表写法
    }; //注意分号

    ListNode* head;

    //初始化链表
    MyLinkedList() {
        //head = new ListNode(0); //错误的初始化
        head = nullptr; //注意初始化
        // PrintLinkedList();
    }
    
    //获取索引为index的结点的取值,index从0开始
    int get(int index) {

        if (head == nullptr) return -1; //***判断head是否为空***
        ListNode* cur = head; //注意,不用为cur申请独立的空间(不用new)
        for(int i = 0; i < index; i++ ){
            if(cur -> next == nullptr) return -1;
            else cur = cur->next;
        }
        return cur->val;
    }
    
    //在链表头部插入一个节点
    void addAtHead(int val) {
        ListNode* insert_node = new ListNode(val);
        insert_node->next = this->head; //插入的节点的指针域指向旧头节点
        this->head = insert_node; //将插入的节点更新为头节点
        // PrintLinkedList();
    }
    
    //在链表尾部插入一个节点
    void addAtTail(int val) {
        ListNode* insert_node= new ListNode(val);
        if (head == nullptr) { //***判断head是否为空***
            head = insert_node;
            return;
        }

        ListNode* cur = head;
        while(cur->next != nullptr){
            cur = cur->next;
        }
        cur->next = insert_node;
        // PrintLinkedList();
    }
    
    //在给定索引处插入一个节点
    void addAtIndex(int index, int val) {

        ListNode* insert_node = new ListNode(val);

        //构造虚拟头节点 统一头节点和其它节点的插入代码
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head; //目前可以通过dummyHead->next和head找到头节点

        ListNode* cur = dummyHead; //此时cur指向的是虚拟头节点

        for(int i = 0; i < index; i++){ //for循环里的边界条件注意下

            if (cur->next == nullptr) return;
            else cur = cur->next;
        }

        insert_node->next = cur->next;
        cur->next = insert_node;

        head = dummyHead->next; //***注意最后更新头节点***
        delete dummyHead; dummyHead = nullptr;
        // PrintLinkedList();
    }
    
    void deleteAtIndex(int index) {

        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;

        for(int i = 0; i < index; i++){ //for循环里的边界条件注意下
            if(cur->next->next == nullptr) return;
            else cur = cur->next;
        }

        ListNode* temp = cur->next; //删除节点
        cur->next = cur->next->next;
        delete temp; temp = nullptr;

        head = dummyHead->next; //注意最后更新头节点
        delete dummyHead; dummyHead = nullptr;
        // PrintLinkedList();
    }

    void PrintLinkedList(){

        ListNode* cur = this->head;
        while(cur != nullptr){
            cout<<cur->val<<" ";
            cur = cur->next;
        }
        cout<<endl;
    }
};

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        
        if(head == nullptr) return nullptr;
        ListNode* pre = NULL; //前一个节点
        ListNode* cur = head; //当前节点
        ListNode* next = cur->next; //后一个节点

        while(cur != nullptr){ //反转
            cur->next = pre; pre = cur;
            cur = next; 
            if(next != nullptr) next = next->next;
        }
        return pre;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值