203. 移除链表元素
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummynode = new ListNode(-1);
dummynode ->next = head;
ListNode* cur = dummynode;
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 = dummynode->next;
delete dummynode;
return head;
}
};
注意两点:虚拟节点以及释放内存。复习一下C++指针,类。简单说下自己的理解不一定对。对于这样一个链表节点:
* 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) {}
里面包含了两个东西,一个是val,一个是指针next。链表就是由这样的节点一个一个的搭起来的,val存数据,next存下一个节点的地址(也可以理解成一根线指向下一个节点),通过->这样的语法,可以操作指针指向的那个对象。
比如,如果head是指向ListNode1的指针,可以通过如下方式操作
head->val //Listnode1里的val
head->next->next //ListNode2里的next指针
707.设计链表
class MyLinkedList {
public:
//节点
class listNode{
public:
int val;
listNode* next;
listNode() = default;
listNode(int val): val(val), next(nullptr){}
};
MyLinkedList(){
_dummy_head = new listNode(-1);
_size = 0;
}
int get(int index) {
if( index>(_size-1) || index<0){
return -1;
}
listNode* cur = _dummy_head->next;
// for(;index>0;index--){
// cur = cur->next;
// }
while(index--){ // 如果--index 就会陷入死循环
cur = cur->next;}
return cur->val;
}
void addAtHead(int val) {
listNode* newnode = new listNode(val);
newnode->next = _dummy_head->next;
_dummy_head->next = newnode;
_size++;
}
void addAtTail(int val) {
listNode* newnode = new listNode(val);
listNode* cur = _dummy_head;
while(cur->next!= nullptr){
cur = cur->next;
}
cur->next = newnode;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size){return;}
if(index == _size){addAtTail(val);
return;}
listNode* newnode = new listNode(val);
listNode* cur = _dummy_head;
for(int i = index; i>0; i--){
cur = cur->next;
}
newnode->next =cur->next;
cur->next = newnode;
_size++;
}
// void addAtIndex(int index, int val) {
// if(index > _size) return;
// if(index < 0) index = 0;
// listNode* newNode = new listNode(val);
// listNode* cur = _dummy_head;
// while(index--) {
// cur = cur->next;
// }
// newNode->next = cur->next;
// cur->next = newNode;
// _size++;
// }
void deleteAtIndex(int index) {
if(index >= _size){return;}
listNode* cur = _dummy_head;
while(index--){
cur = cur->next;
}
listNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;}
private:
int _size;
listNode* _dummy_head;
};
写的带头节点的单链表。别人的写法会更快
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode{
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
MyLinkedList() {
_size = 0;
_dummyHead = new LinkedNode(0);
}
int get(int index) {
if(index >= _size) return -1;
LinkedNode* ptr = _dummyHead;
for(int i = 0; i <= index; i++){
ptr = ptr->next;
}
return ptr->val;
}
void addAtHead(int val) {
LinkedNode *new_node = new LinkedNode(val);
new_node->next = _dummyHead->next;
_dummyHead->next = new_node;
_size += 1;
}
void addAtTail(int val) {
LinkedNode *ptr = _dummyHead;
while(ptr->next != nullptr){
ptr = ptr->next;
}
LinkedNode *new_node = new LinkedNode(val);
ptr->next = new_node;
_size += 1;
}
void addAtIndex(int index, int val) {
if(index > _size) return;
LinkedNode *ptr = _dummyHead;
LinkedNode *new_node = new LinkedNode(val);
for(int i = 0; i < index; i++){
ptr = ptr->next;
}
new_node->next = ptr->next;
ptr->next = new_node;
_size += 1;
}
void deleteAtIndex(int index) {
if(index >= _size) return;
LinkedNode *ptr = _dummyHead;
for(int i = 0; i < index; i++){
ptr = ptr->next;
}
LinkedNode *tmp = ptr->next;
ptr->next = tmp->next;
delete(tmp);
_size -= 1;
}
private:
int _size;
LinkedNode *_dummyHead;
};
206. 反转链表
简单的头插法,但是写起来要注意空指针。代码如下:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* dummyhead = new ListNode(-1);
ListNode* cur = head;
if(cur == nullptr){return head;}
else{
while( cur->next !=nullptr){
ListNode* tmp =cur->next;
cur->next = dummyhead->next;
dummyhead->next = cur;
cur=tmp;
}
cur->next = dummyhead->next;
dummyhead->next = cur;
return dummyhead->next;}
}
};
注意相关的c艹语法,今天涉及到的C艹语法有,类(class与struct,初始化),指针等。