链表基础:文章链接
203. 移除链表元素
题目链接:203.移除链表元素
文档讲解:文档讲解链接
视频讲解:视频讲解链接
代码思路:单向链表移除元素,将当前节点的上一个节点的下一个节点连接上当前节点的下一个节点,注意移除头结点有些特殊,可以设置虚拟节点来省去单独处理头节点的过程。
/**
* 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* dummy_head = new ListNode(0);
dummy_head->next = head;
ListNode* cur = dummy_head;
//这里必须用while,循环判断当前节点的下一个节点是否也满足条件,不能用for
while (cur->next != nullptr) {
if (cur->next->val == val) {
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
} else {
cur = cur->next;
}
}
head = dummy_head->next;
delete dummy_head;
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 < 0 || index >= __size){
return -1;
}
LinkedNode* cur = __dummyHead;
for(int i = 0; i < index; i++) {
cur = cur->next;
}
return cur->next->val;
}
void addAtHead(int val) {
LinkedNode* tempNode = new LinkedNode(val);
tempNode->next = __dummyHead->next;
__dummyHead->next = tempNode;
__size++;
}
void addAtTail(int val) {
LinkedNode* cur = __dummyHead;
while (cur->next != nullptr) {
cur = cur->next;
}
LinkedNode* tempNode = new LinkedNode(val);
cur->next = tempNode;
__size++;
}
void addAtIndex(int index, int val) {
if (index > __size) return;
if (index < 0) index = 0;
LinkedNode* cur = __dummyHead;
for (int i = 0; i < index; i++) {
cur = cur->next;
}
LinkedNode* tempNode = new LinkedNode(val);
tempNode->next = cur->next;
cur->next = tempNode;
__size++;
}
void deleteAtIndex(int index) {
if (index >= __size || index < 0) {
return;
}//注意这里需要判断无返回值
LinkedNode* cur = __dummyHead;
for (int i = 0; i < index; i++) {
cur = cur->next;
}
LinkedNode* tempNode = cur->next;
cur->next = cur->next->next;
delete tempNode;
tempNode = nullptr;
__size--;
}
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.反转链表
代码思路:通过递归将链表的指针反向。
/**
* 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* reverseRecursion(ListNode* pre, ListNode* cur) {
if (cur == nullptr) {//递归的结束条件
return pre;
}
ListNode* temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
// delete temp;
return reverseRecursion(pre, cur);
}
ListNode* reverseList(ListNode* head) {
ListNode* pre = nullptr;
ListNode* cur = head;
return reverseRecursion(pre, cur);
}
};