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) {
// 当目标值在首节点时
while(head!=nullptr&&head->val==val){
//存储head节点地址
ListNode* tmp = head;
head = head->next;
// 释放内存
delete tmp;
};
// 当目标值不在首节点时
ListNode* cur = head;
while(cur!=nullptr&&cur->next!=nullptr){
if(cur->next->val==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,head);
ListNode* cur = dummyHead;
while(cur!=nullptr&&cur->next!=nullptr){
if(cur->next->val ==val){
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}else{
cur = cur->next;
};
};
return dummyHead->next;
}
};
707.设计链表(初次写有点难)
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode():val(0),next(nullptr){};
LinkedNode(int val):val(val), next(nullptr){};
LinkedNode(int val,LinkedNode* next):val(val),next(next){};
};
// 初始化链表
MyLinkedList(){
_size = 0;
_dummyHead = new LinkedNode();
}
int get(int index) {
if(index<0||index>_size-1){
return -1;
}
LinkedNode* cur = _dummyHead;
while(index){
cur = cur->next;
index--;
};
return cur->next->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;
LinkedNode* newNode = new LinkedNode(val);
while(cur->next!=nullptr){
cur=cur->next;
};
cur->next = newNode;
// newNode->next = nullptr;
_size++;
}
void addAtIndex(int index, int val) {
// LinkedNode* cur = _dummyHead;
// if(index<0){
// index = 0;
// }
// if(-1<index<_size){
// while(index){
// cur=cur->next;
// index--;
// };
// LinkedNode* newNode = new LinkedNode(val);
// newNode->next = cur->next;
// cur->next = newNode;
// _size++;
// };
if(index > _size) return;
if(index < 0) index = 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(-1<index<_size){
// LinkedNode* cur = _dummyHead;
// while(index){
// cur = cur->next;
// index--;
// }
// LinkedNode* tmp = cur->next;
// cur->next = cur->next->next;
// delete tmp;
// //delete命令指示释放了tmp指针原本所指的那部分内存,
// //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
// //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
// //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
// tmp=nullptr;
// _size--;
// }else{
// return;
// }
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;
//delete命令指示释放了tmp指针原本所指的那部分内存,
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
tmp=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.反转链表
//双指针解法
// class Solution {
// public:
// ListNode* reverseList(ListNode* head) {
// ListNode*cur = head;
// ListNode*pre = nullptr;
// while(cur!=nullptr){
// ListNode* temp = cur->next;
// cur->next = pre;
// pre = cur;
// cur = temp;
// }
// return pre;
// }
// };
// 递归双指针
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode*cur){
if(cur == nullptr){
return pre;
};
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur,temp);
};
ListNode* reverseList(ListNode* head) {
return reverse(nullptr,head);
}
};