LeetCode203移除链表元素
思路:移除链表元素也就是删除链表元素,直接将要删除的节点的前一个元素指向它的后一个元素,然后释放相应的内存空间。
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;
delete dummyHead;
return head;
}
};
LeetCode707设计链表
思路:本题要求设计链表的删除、头插、尾差以及在任意位置插入元素,需要明确边界条件,同时注意不要断链。
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;
newNode->next=nullptr;
size++;
}
void addAtIndex(int index, int val) {
if(index>size){
return;
}
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 printLnkedList(){
LinkedNode* cur=dummyHead;
while(cur->next!=nullptr){
cout<<cur->next->val<<"";
cur=cur->next;
}
cout<<endl;
}
private:
int size;
LinkedNode* dummyHead;
};
本题中一定要注意判断指针是否为空!!
206反转链表
思路:我认为可以创建一个新的链表然后将现有的链表进行遍历之后就可以得到新的链表。然后觉得不是好方法所以就没有实现。
双指针法:这个思路一开始我并没有想到,看了代码随想录之后才想到可以将指针的指向直接改变,这样就可以完成链表的反转。
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;
}
};
递归法:递归法其实就是将双指针法中的pre=cur;cur=temp使用reverse函数完成,以及cur和pre的初始化。
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);
}
};
总结:通过今天的学习,我了解到了虚拟头结点的意义,是为了便于对所有节点进行插入删除等操作。同时熟悉了链表的各种操作,最后在链表反转上拓宽了我的思路,了解了链表逆置可以直接将指针的指向改变。