203.移除链表元素
● 今日学习的文章链接和视频链接
● 自己看到题目的第一想法
每删除一个元素后,把它前后的元素连起来。
● 看完代码随想录之后的想法
假如要删除第一个节点,将head指向下一个节点。或者使用虚拟头节点。
● 自己实现过程中遇到哪些困难
使用原链表:
本来有些没有理解最后一个节点若是和val一样要怎么被删除,因为他的next是null。才意识到,如果最后一个节点最后变成头节点,看代码可知也可以被删。要是不是头节点,头节点的next不是null,所以最后一个节点还是可以成功被删除。
/**
* 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* tmp = head;
head = head->next;
delete tmp;//释放内存
}
ListNode* cur = head;
while(cur != NULL&& cur->next != NULL){
if(cur->next->val == val){
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}
else{
cur = cur->next;
}
}
return head;
}
};
使用虚拟头节点:
/**
* 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) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = 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;
delete dummyHead;
return head;
}
};
● 今日收获,记录一下自己的学习时长
1h
707.设计链表
● 今日学习的文章链接和视频链接
● 自己看到题目的第一想法
最后两个有点懵
● 看完代码随想录之后的想法
最重要的是判断好n是cur->next还是什么。
● 自己实现过程中遇到哪些困难
这道题整合链表很多知识点,需要多次温习巩固。
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;//小于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;
};
/**
* 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);
*/
● 今日收获,记录一下自己的学习时长
1h
206.反转链表
● 今日学习的文章链接和视频链接
● 自己看到题目的第一想法
0 0
● 看完代码随想录之后的想法
双指针+临时指针或递归。
● 自己实现过程中遇到哪些困难
理解了之后就不难了。
双指针:
/**
* 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;
}
};
递归:
/**
* 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* reverse(ListNode* pre, ListNode* cur){
if(cur == NULL) return pre;
ListNode* tmp = cur->next;
cur->next = pre;
return reverse(cur,tmp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL,head);
}
};
● 今日收获,记录一下自己的学习时长
1h