1.链表理论基础
2.移除链表元素
题目链接/文章讲解/视频讲解::代码随想录
代码:直接在原链表上操作
/**
* Definition for singly-linked list.
* 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 != NULL && head->val == val){
ListNode* temp = head;
head = head->next;
delete temp;
}
ListNode* cur = head;
while(cur != NULL && cur->next != NULL){
if(cur->next->val == val){
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
}else{
cur = cur->next;
}
}
return head;
}
};
note:
救命了!!我老是忘记要删除的节点是cur->next。。。只能找到要操作节点的前一个结点进行操作啊啊啊!!!
代码:虚拟头结点
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
ListNode* cur = dummyhead;
while(cur->next != NULL){
if(cur->next->val == val){
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
}else{
cur = cur->next;
}
}
return dummyhead->next;
}
};
note:
真的就是一夜回到解放前,啥也不会了。大体思路是知道的,但是连链表的创建都忘光光了。而且!!这里有一个需要注意的点!!头结点有可能会被删除掉,因此返回的是dummyhead->next。
3.设计链表
题目链接/文章讲解/视频讲解:代码随想录
代码:
class MyLinkedList {
public:
struct LinkedNode{
int val;
LinkedNode* next;
LinkedNode(int val):val(val),next(nullptr){}
};
MyLinkedList() {
dummyhead = new LinkedNode(0);
size = 0;
}
// index是从0开始的,可以带入数值去试
int get(int index) {
if(index < 0 || index > size - 1){
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* cur = dummyhead;
while(cur->next != NULL){
cur = cur->next;
}
LinkedNode* newNode = new LinkedNode(val);
cur->next = newNode;
size++;
}
void addAtIndex(int index, int val) {
if(index > size || index < 0){
return;
}
// 要找到操作结点的前一个结点
LinkedNode* cur = dummyhead;
while(index--){
cur = cur->next;
}
LinkedNode* newNode = new LinkedNode(val);
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* temp = cur->next;
cur->next = cur->next->next;
delete temp;
size--;
}
private:
LinkedNode* dummyhead;
int size;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
note:还行,熟悉熟悉就可以了。记住每次都是找到要操作结点的前一个结点!!
4.反转链表
题目链接/文章讲解/视频讲解:代码随想录
代码:
/**
* Definition for singly-linked list.
* 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* 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;
}
};
note:
错错错!!!
思路还记得,但是边界处理是完全不行啊。好恨!!就是想办法把箭头方向换一下。什么时候处理了所有的箭头(为了处理所有的箭头,因此cur最后执行的时候是指向最后一个结点,最后一个也要处理,因此是cur!=NULL),在最后返回的时候,因为已经循环完了,所以这时候cur肯定指向了空结点,那么我们就返回pre。
这样解释应该就不会分不清边界条件了。。