203.移除链表元素
空指针
空指针指的是一个指向内存中空地址的指针。在计算机程序中,指针是用来存储和操作内存地址的变量。如果一个指针被赋予了空地址(0或NULL),那么它就成为了一个空指针。
在程序中,如果使用了一个空指针,就会出现错误,因为程序试图访问一个不存在的内存地址。这种错误通常被称为“空指针引用”,会导致程序崩溃或产生其他不可预测的行为。
因此,在编写程序时,应该始终检查指针是否为空,以避免空指针引用导致的问题。
ListNode:
ListNode是在数据结构中常用的一种节点类型,用于实现链表这种数据结构。链表是一种线性数据结构,其中每个节点包含一个值和一个指向下一个节点的指针。与数组不同,链表的节点在内存中不必彼此相邻,每个节点可以在任何地方分配。链表通常用于需要频繁插入或删除元素的场景,因为这些操作在链表中具有O(1)的时间复杂度。
在实现链表时,每个节点都需要存储一个值和一个指向下一个节点的指针。ListNode就是用来表示链表节点的数据结构,通常包含两个成员变量:val用于存储节点的值,next用于存储指向下一个节点的指针。在编写链表相关的算法时,我们通常需要创建和操作多个ListNode对象来构建链表,并对链表进行遍历、插入、删除等操作。
下面是一个简单的示例,展示了如何使用ListNode来创建一个链表:
class ListNode {
public:
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* head = new ListNode(1); // 创建链表头节点
ListNode* node1 = new ListNode(2); // 创建第一个节点
ListNode* node2 = new ListNode(3); // 创建第二个节点
head->next = node1; // 链接头节点和第一个节点
node1->next = node2; // 链接第一个节点和第二个节点
方法一:直接用原来的链表进行删除
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;
}
};
方法二:使用虚拟头
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;
}
};
707.设计链表
掌握这题基本上就掌握了链表的基础操作,各种小细节要注意
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* cur = DummyHead;
LinkedNode* Newnode = new LinkedNode(val);
while (index--) {
cur = cur->next;
}
NewMode->next = cur->next;
cur->next = NewMode;
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;
size--;
}
void printLinedList() {
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);
*/
206.反转链表
方法一:双指针法
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;
}
};
方法二:递归法
class Solution {
public:
ListNode* reverse(ListNode* pre, ListNode* cur) {
if (cur == NULL)return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur, temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL, head);
}
};
递归法需要再回顾回顾,加油!