户外媒体网站建设免费/seo文章生成器
代码随想录算法训练营二刷day3| 203.移除链表元素 、707.设计链表、206.反转链表
链表基础
链表节点定义:
单链表
struct ListNode {int val; //结点上存储元素ListNode *next; //指向下一个节点的指针ListNode(int x) : val(x), next(nullptr) {} //结点的构造函数
};
LeetCode 203 移除链表元素
题目链接: 203.移除链表元素
设置虚拟头结点,然后统一进行删除操作
class Solution {
public:ListNode* removeElements(ListNode* head, int val) {//首先创建虚拟头节点ListNode *dummyHead = new ListNode(0);dummyHead->next = head;ListNode *cur = dummyHead; //保存虚拟头节点,方便使用while(cur->next) { //保证虚拟头节点指向的下个节点非空if(cur->next->val == val) {ListNode *tmp = cur->next;cur->next = cur->next->next;delete(tmp);}else cur = cur->next;}head = dummyHead->next;delete(dummyHead);return head;}
};
本题小结:单链表节点定义,虚拟头节点的设置、链表的遍历以及删除结点释放空间。
LeetCode 707题 设计链表
题目链接: 707.设计链表
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
class MyLinkedList {
public://定义链表结点结构体struct LinkedNode {int val;LinkedNode *next;LinkedNode(int x) : val(x), next(nullptr) {}};//初始化链表MyLinkedList() {_dummyHead = new LinkedNode(0); //虚拟头节点_size = 0;}//获取到第index个结点数值,如果index是非法数值直接返回-1int get(int index) {if (index < 0 || index > _size - 1) //首先判断index的值是否不在有效范围内return -1;LinkedNode *node = _dummyHead->next;while (index--) {node = node->next;}return node->val;}//在链表最前面添加一个结点void addAtHead(int val) {LinkedNode *newnode = new LinkedNode(val);newnode->next = _dummyHead->next;_dummyHead->next = newnode; //此处需要注意添加结点的顺序_size++;}//在链表最后添加一个结点void addAtTail(int val) {LinkedNode *newnode = new LinkedNode(val);LinkedNode *cur = _dummyHead;while (cur->next) {cur = cur->next;}cur->next = newnode;_size++;}// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点// 如果index大于链表的长度,则返回空// 如果index小于0,则在头部插入节点void addAtIndex(int index, int val) {if (index > _size) return;if (index < 0) index = 0;LinkedNode *newnode = new LinkedNode(val);LinkedNode *cur = _dummyHead;while (index--) { //注意:因为cur取虚拟头节点,所以自减在前 当不确定时可以取一个index的值试cur = cur->next;}newnode->next = cur->next;cur->next = newnode;_size++;}//删除第index个结点,如果index大于链表的长度,直接return,index从0开始void deleteAtIndex(int index) {if (index > _size) return;LinkedNode *cur = _dummyHead;while (index--) { //删除要注意需要遍历到删除的前一个结点cur = cur->next;}LinkedNode *tmp = cur->next;cur->next = cur->next->next;delete(tmp);_size--;}void printfNode() {LinkedNode *node = _dummyHead->next;while (node) {cout << node->val << " ";node = node->next;}cout << "" << endl;}private:int _size;LinkedNode* _dummyHead;
};
本题小结:在添加和删除时需要注意根据index判断遍历到哪个位置(要在有效范围内),同时要将长度加1或减1。
LeetCode 206题 反转链表
题目链接: 206.反转链表
思路:定义一个cur指针指向头结点,定义一个pre指针初始化为null,用tmp不断保存cur->next,然后cur->next指向pre,pre再向后移动,循环直到cur指向NULL。
双指针法
class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode* temp;ListNode* cur = head;ListNode* pre = NULL;while(cur) {temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;}
};
递归法
class Solution {
public:ListNode* reverse(ListNode* pre, ListNode* cur) {if(cur == NULL) return pre;ListNode* temp = cur->next;cur->next = pre;return reverse(cur,temp);}ListNode* reverseList(ListNode* head) {return reverse(NULL, head);}
};