代码随想录算法训练营第三天| 链表理论基础、203.移除链表元素、707.设计链表、206.反转链表
链表理论基础
文档讲解:关于链表,你该了解这些!
视频讲解:无
目标:理解链表基础,以及链表和数组区别
状态:😸
链表要点
链表中存在若干节点,每一个节点由数据和指针组成,通过指针将其串联成线性结构。
链表有一个入口节点,我们把它叫做头节点head,一般来说,头节点中不含有数据,它作为一个指示符指向链表中含有数据的第一个节点。
2024/2/23
注意,在其他情况下,头节点可能就是链表的第一个节点,比如下面的题目
链表的几种类型:
单链表:一个指针域,一个数据域
双链表:两个指针域(分别指向前置和后继节点),一个数据域
循环链表:首尾相连的链表,可以是单链表也可以是双链表
存储方式:
数组在内存中连续存储,链表在内存中散列存储,有效的利用了内存中的碎片化空间。学过操作系统的话,可能会知道fragment这个名词,意思就是碎片的意思。
C++相关
链表的定义
1 2 3 4 5 6 7
| struct ListNode { ElemType data; ListNode *next; ListNode(ElemType data) : data(data), next(nullptr) {} };
|
使用示例:ListNode *head = new ListNode(null);
注意:在c++中,当删除元素时需及时释放内存空间,delete tmp。
203.移除链表元素
题目链接:203.移除链表元素
文档讲解:203.移除链表元素
视频讲解:手把手带你学会操作链表 | LeetCode:203.移除链表元素
目标:理解 虚拟头结点的使用技巧,这个对链表题目很重要。
状态:刚开始对题目中头节点的定义没理解清楚
学习前的想法
该题需要删除链表中所有值等于val的节点。
看到这一题,我的第一个想法是使用快慢指针,即定义 快指针fast和 慢指针slow,始终保持fast = slow + 1,并使用快指针遍历链表。
需要注意,由于第一个节点涉及到了链表的头节点问题,需要单独进行一个判断,即如果头节点指向的位置的值为val,则直接操作头节点,然后再进行上述快慢指针操作。
尝试写出代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(head == nullptr) return head;
head = head->next;
while(head->val == val) head = head->next;
ListNode *slow = head; ListNode *fast = slow->next;
while(fast != nullptr && slow != nullptr) { if(fast->val == val) { slow->next = fast->next; fast = slow->next; } slow = slow->next; if(fast != nullptr) fast = slow->next; } return head; } };
|
以上代码为错误代码runtime error: member access within null pointer of type 'ListNode'。
2024/2/23
似乎对头指针的理解有些误会,导致代码持续报错
学习后的想法
移除头节点和移除其他节点的不一样,需要做一个判断。
和我上面想的一样,但是我的快慢指针的想法太冗余了,只需要额外声明一个指针即可实现上述操作。
2024/2/23
在卡哥的视频中,头节点似乎是指链表的第一个节点,包含有数值。
之前我理解的是头节点不包含数值,而是指向链表的第一个节点
使用虚拟头节点:
首先实例化一个虚拟头节点,让其指向原来链表的head。
定义临时指针指向虚拟头节点,用以遍历整个链表。
这样可以统一化原始链表中所有节点的操作,不需要再向上面一样判断指针指向的是否是头节点。
实现
原链表删除
1 2 3 4 5 6 7 8 9
| while(head != NULL && head->val = val) head = head->next;
cur = head; while(cur != null && cur->next != null) { if(cur->next->val == val) cur->next = cur->next->next; else cur = cur->next; }
return head;
|
虚拟头节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // 定义虚拟头节点 ListNode *dummyHead = new ListNode(); dummyHead->next = head; ListNode *cur = dummyHead; // 声明指针遍历链表
while(cur->next != null) { if(cur->next->val == val) { cur->next = cur->next->next; } else { cur = cur->next; } }
return dummyHead->next;
|
707.设计链表
题目链接:707.设计链表
文档讲解:707.设计链表
视频讲解:帮你把链表操作学个通透!LeetCode:707.设计链表
目标:链表综合操作
状态:C++的语法不是很熟悉,具体的函数写法基本清楚
学习前的想法
上一题中学习了虚拟头节点的使用,卡哥对这一题的建议也是使用虚拟头节点。因此我将会直接考虑虚拟头节点的思路,也会尝试去使用虚拟头节点解决该问题。
根据题目,需要使用到虚拟头节点的地方应该只有void addAtIndex(int index, int val)和void deleteAtIndex(int index)
学习后的想法
其实这一题没什么好说的,等以后有了新想法再说吧。(还是得熟悉一下C++类的语法)
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| 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 > (_size - 1) || index < 0) { return -1; } LinkedNode* cur = _dummyHead->next; while(index--){ cur = cur->next; } return cur->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 != nullptr){ cur = cur->next; } cur->next = newNode; _size++; }
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 = cur->next; } newNode->next = cur->next; cur->next = newNode; _size++; }
void deleteAtIndex(int index) { if (index >= _size || index < 0) { return; } LinkedNode* cur = _dummyHead; while(index--) { cur = cur ->next; } LinkedNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; tmp=nullptr; _size--; }
void printLinkedList() { LinkedNode* cur = _dummyHead; while (cur->next != nullptr) { cout << cur->next->val << " "; cur = cur->next; } cout << endl; } private: int _size; LinkedNode* _dummyHead;
};
|
206.反转链表
题目链接:206.反转链表
文档讲解:206.反转链表
视频讲解:206.反转链表
目标:理解反转链表
状态:双指针法的思路不是很难,重点是递归的逻辑问题
学习前的想法
顾名思义,反转链表,将一个单链表反转。
我的想法是声明一个空节点,让其指向头节点,声明一个指针遍历链表,将遍历的值存入空节点,并让其指向空节点的下一节点,空节点则指向这个节点。
思路似乎没有什么问题,尝试着取实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *node = new ListNode(0, head); ListNode *cur = head->next; head->next = nullptr; while(cur != nullptr) { cur->next = node->next; node->next = cur; }
return node->next; } };
|
2024/2/23
代码超时,不知道原因。。。果断放弃,去学习视频。
学习后的想法
双指针法:
首先定义一个cur指针,指向头节点,再定义一个pre指针,初始化为null。
接着开始反转,首先需要将cur->next使用tmp指针保存一下,因为一会反转之后,cur和它的后一个节点之间的联系就断开了。反转一次之后,需要将pre和cur都向后移动一位。
循环以上过程,当cur指针为空时,说明链表反转完毕,此时pre指向链表的新的头节点,返回pre即可。
递归:
递归法实际上思路和双指针法是一致的,只是采用了递归的实现方式。
实现
双指针法
1 2 3 4 5 6 7 8 9 10 11
| cur = head; pre = nullptr;
while(cur != nullptr) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; }
return pre;
|
递归法(建议参考上述双指针法思路完成)
1 2 3 4 5 6 7 8 9 10 11 12
| reverse(pre, cur) { if(cur == null) return pre; tmp = cur->next; cur->next = pre; return reverse(cur, tmp); }
reverseList(head) { return reverse(NULL, head); }
|
收获与学习时长
学习时长: 2h 50min
收获
复习了链表基础,以及链表和数组的区别
学习了虚拟头节点的使用
学会了反转链表的双指针法和递归法