打卡第三天:移除链表元素、设计链表、反转链表

一、链表基础

1、为什么要用链表

太大的数组占用空间,删除插入效率低下。

链表是链式存储结构,动态管理,资源重复利用。但需要分配另外的空间存储前驱和后继所在位置。

2、结构体指针

两种定义方式

struct 结构体类型名
    {
        成员表;
        成员函数;
    }结构体指针变量表;
// 或:};
// 结构体名 结构体指针变量表;

例如:struct student
    {
        string name;
        int chinese, math, total;
    }*stu1;
// 或:};
// student *stu1;

结构体指针引用

指针名->成员名
(*stu1).chinese等价于stu1->chinese

自引用结构

struct stu
{
    char name[20];
    int age, score;
    stu *p;//不能用p
};

3、单链表基本结构

struct Node{
    int data;//数据域
    Node *next;//指针域,指向下一个结点
};
Node *head, *p, *r;
int x;

int main(){
    cin >> x;
    head = new Node;//申请头结点
    r = head;
    while (x!=-1){
        p = new Node;
        p->data = x;
        p->next = nullptr;
        r->next = p;//把新结点链接到前面的链表中
        r = p;
        cin>>x;
    }
    p = head->next;
    while(p->next! = nullptr){
        cout<<p->data<<" ";//输入链表元素
        p = p->next;
    }
    cout<<p->data<<endl;
    return 0;    
}

一般用指向第一个结点的指针表示链表

4、几类链表

单链表:通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null。链表的头结点是head。

图源 下同

双链表:每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。双链表既可以向前查询也可以向后查询。

循环链表:链表首尾相连。循环链表可以用来解决约瑟夫环问题。

链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。

如果不定义构造函数使用默认构造函数的话,在初始化的时候就不能直接给变量赋值。

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

二、题目

1、移除链表元素

题目

文章

视频

直接使用原链表操作

#include <iostream>
#include <vector>
using namespace std;

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) {
        // 删除头结点
        while (head != nullptr && head->val == val) { // 注意这里不是if
            ListNode* tmp = head;
            head = head->next;
            delete tmp;
        }

        // 删除非头结点
        ListNode* cur = head;
        while (cur != nullptr && cur->next != nullptr) {
            if (cur->next->val == val) {
                ListNode* tmp = cur->next;
                cur->next = cur->next->next;//cur下下个结点赋给要删除的下个结点
                delete tmp;
            } else {
                cur = cur->next;
            }
        }
        return head;
    }
};

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

int main() {
    vector<int> values = {1,2,6,3,4,5,6};
    ListNode* head = nullptr;
    ListNode* cur = nullptr;
    for (int val : values) {
        if (!head) {//通过检查 head 是否为空来决定是创建第一个节点还是添加新的节点。
            head = new ListNode(val);
            cur = head;
        } else {
            cur->next = new ListNode(val);
            cur = cur->next;
        }
    }
    int val = 6;// 目标值
    cout << "Original list: ";
    printList(head);// 打印原链表
    Solution solution; // 创建解决方案实例
    ListNode* newHead = solution.removeElements(head, val);// 删除节点值为val的节点
    cout << "Modified list: ";
    printList(newHead);// 打印修改后的链表

    return 0;
}

设置一个虚拟头结点再删除

#include <iostream>
#include <vector>
using namespace std;

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* dummyHead = new ListNode(0); //实例化
        // 设置一个虚拟头结点,是为了删除各结点都是相同的操作而不需要额外判断。
        dummyHead->next = head; // 将虚拟头结点指向head
        ListNode* cur = dummyHead;
        while (cur->next != NULL) {
            if(cur->next->val == val) {
                ListNode* tmp = cur->next;
                cur->next = cur->next->next;//利用tmp帮助调整和删除
                delete tmp;
            } else {
                cur = cur->next;
            }
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

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

int main() {
    vector<int> values = {1, 2, 6, 3, 4, 5, 6 };
    ListNode* dummyHead = new ListNode(0);
    ListNode* cur = dummyHead;
    for (int val : values) { //遍历并构建链表
        cur->next = new ListNode(val);
        cur = cur->next;
    }
    int val = 6;// 目标值
    cout << "Original list: ";
    printList(dummyHead->next);// 打印原链表
    Solution solution; // 创建解决方案实例
    ListNode* newHead = solution.removeElements(dummyHead->next, val); // 删除节点值为val的节点
    cout << "Modified list: ";
    printList(newHead);  // 打印修改后的链表
    delete dummyHead;// 清理虚拟头结点

    return 0;
}

关键:头结点的指针是不能改的,遍历链表时要设置一个临时指针current。删除元素时一定要知道这个元素的上一个元素的指针是什么,上一个只能是current,删的也就只能是current的next,这样才能让current直接指向current的next的next。所以current要直接指向dummyHead而不是dummyHead的next。

2、设计链表

题目

文章

视频

class MyLinkedList {
public:
    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) {}
    };

    MyLinkedList() {
        dummyHead = new ListNode(0);
        size = 0;
    }
    
    int get(int index) {
        if (index > (size - 1) || index < 0 ){
            return -1;
        }
        ListNode *cur = dummyHead->next;
        while (index--){
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        ListNode *newNode = new ListNode(val);
        newNode->next = dummyHead->next;
        dummyHead->next = newNode;
        size++;
    }
    
    void addAtTail(int val) {
        ListNode *newNode = new ListNode(val);
        ListNode *cur = dummyHead;
        while (cur->next != nullptr){
            cur = cur->next;
        }
        cur->next = newNode;
        size++;
    }
    
    //在index的结点前插入
    void addAtIndex(int index, int val) {
        if(index > size)return;//注意这句的写法。index大于链表长度,返回空
        if(index < 0) index = 0;//如果index小于0 就在头部插入结点
        ListNode *newNode = new ListNode(val);
        ListNode *cur = dummyHead;
        while (index--){//根据index让链表next找到对应的结点
            cur = cur->next;
        }
        newNode->next = cur->next;//让新结点指向找到的结点
        cur->next = newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= size - 1 || index < 0 ){
            return;
        }
        ListNode *cur = dummyHead;
        while (index--){
            cur = cur->next;
        }
        ListNode *tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tmp = nullptr;//防止野指针
        size--;
    }
    
    void PrintList(){
        ListNode* cur = dummyHead;
        while (cur->next != nullptr) {
            cout << cur->next->val << " ";
            cur = cur->next;
        }
        cout << endl;
    }

private:
    int size;
    ListNode* dummyHead;

};

不熟练。

3、反转链表

题目

文章

视频

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *tmp;
        ListNode *cur = head;
        ListNode *pre = nullptr;
        while (cur){
            tmp = cur->next;//保存,因为下一行需要改变cur->next
            cur->next = pre;//反转
            pre = cur;//更新
            cur = tmp;//更新
        }
        return pre;
    }
};

结构体和链表的基础知识不熟练。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值