文章目录
链表理论基础
学习链接:链表理论基础
链表的存储方式
链表在内存中不是连续分布的
链表的定义
struct ListNode(
int val; // 节点上存储的元素
ListNode *next; // 指向下一个节点的指针
ListNode(int x):val(x),next(NULL){} //节点构造函数
);
// 这样的定义方式可以直接初始化的时候给变量赋值
ListNode* head = new ListNode(5);
链表的操作
删除节点
需要从头开始查询找到C节点,将C节点的next指针指向E节点
c++最好手动释放内存
添加节点
性能分析
203.移除链表元素
题目链接:203.移除链表元素
文章讲解:代码随想录|203.移除链表元素
视频讲解:手把手带你学会操作链表 | LeetCode:203.移除链表元素
思路
删除链表元素是将上个元素的next指向删除元素的next,但如果删除的头节点,就无法直接cur->next = cur->next->next,因此要么可以分情况讨论,要么可以设置一个虚拟头结点来统一逻辑
设置虚拟头结点
写代码需要注意的点:
- 头结点指针不能修改,因为最后要返回头指针,所以要设置一个cur=dummyhead,又因为可能删除的是head且删除需要cur为上一个元素,所以不能是cur=dummyhead->next。
- return dummyNode->next;而不是return head,因为有可能删除的是head。
- c++要记得清理内存
时刻牢记:指针有没有可能指向NULL来写循环条件,且一定要考虑删除头指针的情况
代码
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;
delete tmp;
} else {
cur = cur->next;
}
}
head = dummyHead->next; // 有可能head已经被删了,所以不能直接return head
delete dummyHead;
return head;
}
};
707.设计链表
题目链接:707.设计链表
文章讲解:代码随想录|707.设计链表
视频讲解:帮你把链表操作学个通透!LeetCode:707.设计链表
思路
熟悉链表的基础操作,此题采用虚拟头结点
虚拟头结点:指向头结点,dummyhead为虚拟头结点地址,dummyhead->next为头结点(第一个节点)地址
头结点:第一个节点(val,next),head为头结点地址
此题存在index和size之间关系的判断,所以每次增/删操作时候要记得size++/–
代码
class MyLinkedList {
public:
struct LinkedNode{
int val;
LinkedNode* next;
LinkedNode(int x):val(x),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* node=new LinkedNode(val);
node->next=dummyHead->next;
dummyHead->next = node;
_size++;
}
void addAtTail(int val) {
LinkedNode* node=new LinkedNode(val);
LinkedNode* cur=dummyHead;
while(cur->next!=nullptr){
cur=cur->next;
}
cur->next=node;
_size++;
}
void addAtIndex(int index, int val) {
if(index>_size)return;
LinkedNode* node = new LinkedNode(val);
LinkedNode* cur = dummyHead; //cur要指向index上一个,这样cur->next指向新node
while(index--){
cur = cur->next;
}
node->next = cur->next;
cur->next = node;
_size++;
}
void deleteAtIndex(int index) {
if(index>_size-1||index<0){
return;
}
LinkedNode* cur = dummyHead;
while(index--){
cur = cur->next;
}
LinkedNode* tmp = cur->next;
cur->next=cur->next->next;
delete tmp;
tmp=nullptr;
//delete命令指示释放了tmp指针原本所指的那部分内存,
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
_size--;
}
private: // 类中的变量,MyLinkedList(){}初始化的时候如果重复声明,则不会初始化类中的变量
int _size;
LinkedNode* dummyHead;
};
206.反转链表
题目链接:206.反转链表
文章讲解:代码随想录|206.反转链表
视频讲解:帮你拿下反转链表 | LeetCode:206.反转链表 | 双指针法 | 递归法
思路
双指针法
原地改变指针方向
两个指针:pre和cur
初始化为pre=null, cur=head,然后都一步一步往后移,改变指针的指向
递归法
根据双指针的代码写
单指针法
自己想的,就是一个元素一个元素的移动到头结点位置
代码
双指针法
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* temp; // 保存cur的下一个节点
ListNode* cur = head;
ListNode* pre = NULL;
while(cur) {
temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next
cur->next = pre; // 翻转操作
// 更新pre 和 cur指针
pre = cur;
cur = temp;
}
return pre;
}
};
保存cur下一个节点的时候直接temp = cur->next;就行,不要只保存cur再想着后面取->next,因为后面再取的话就不是当时那个cur后面的元素了,这样写就是错误的:
while(cur != null){
ListNode tmp = cur;
cur.next = pre;
pre = cur;
cur = tmp.next;
}
递归法
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
// 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步
// pre = cur;
// cur = temp;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
// 和双指针法初始化是一样的逻辑
// ListNode* cur = head;
// ListNode* pre = NULL;
return reverse(NULL, head);
}
};
单指针法
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == nullptr){
return head;
}
ListNode* cur = head;
ListNode* tmp = nullptr;
while(cur->next!=nullptr){
tmp = head;
head = cur->next;
cur->next = cur->next->next;
head->next = tmp;
}
return head;
}
};
今日收获
- 根据指针有没有可能指向NULL来写循环条件
- 如果是删除某个元素的话指针要指向上一个的时候来删(cur->next=cur->next->next)
- 链表题别干想,多指针画图,删除了什么指针,模拟while循环过程