/** * 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 != NULL && head->val == val){ ListNode* tmp = head; head = head->next; delete tmp; } ListNode* new_head = head; while(head != NULL){ if(head->next != nullptr && head->next->val == val){ ListNode* tmp = head->next; head->next = head->next->next; delete tmp; }else{ head = head->next; } } return new_head; */ //虚拟头结点 ListNode* tmp_head = new ListNode(0); tmp_head->next = head; ListNode* cur = tmp_head; while(cur != NULL){ if(cur->next != nullptr && cur->next->val == val){ ListNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; }else{ cur = cur->next; } } head = tmp_head->next; delete tmp_head; return head; } };
class MyLinkedList { public: struct my_list{ int val; my_list* next; my_list(int val):val(val),next(nullptr){} }; MyLinkedList() { dummy_list = new my_list(0); _size = 0; } void log(){ return; my_list* tmp = dummy_list; while(tmp != nullptr){ cout << tmp->val << "," ; tmp = tmp->next; } cout << endl; } int get(int index) { //log(); if(_size == 0 || index < 0 || index >= _size) return -1; my_list* tmp = dummy_list->next; while(index --) tmp = tmp->next; return tmp->val; } void addAtHead(int val) { my_list* tmp = new my_list(val); tmp->next = dummy_list->next; dummy_list->next = tmp; _size++; log(); } void addAtTail(int val) { my_list* tmp = dummy_list; while(tmp->next != NULL) tmp = tmp->next; my_list* tmp2 = new my_list(val); tmp->next = tmp2; _size++; log(); } void addAtIndex(int index, int val) { //cout << "size:" << _size << endl; if(index > _size) return; my_list* tmp = dummy_list; while(--index >= 0) tmp = tmp->next; //cout << "index:" << index << endl; my_list* tmp2 = new my_list(val); if(tmp->next != NULL) tmp2->next = tmp->next; tmp->next = tmp2; _size++; log(); } void deleteAtIndex(int index) { if(index >= _size) return; my_list* tmp = dummy_list; while(--index >= 0) tmp = tmp->next; if(index < _size - 1){ my_list* tmp2 = tmp->next; tmp->next = tmp->next->next; delete tmp2; }else{ tmp->next = tmp->next->next; } _size--; log(); } private: my_list* dummy_list; int _size; }; /* class MyLinkedList { public: // 定义链表节点结构体 struct LinkedNode { int val; LinkedNode* next; LinkedNode(int val):val(val), next(nullptr){} }; // 初始化链表 MyLinkedList() { _dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点 _size = 0; } // 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点 int get(int index) { if (index > (_size - 1) || index < 0) { return -1; } LinkedNode* cur = _dummyHead->next; while(index--){ // 如果--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++; } // 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。 // 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点 // 如果index大于链表的长度,则返回空 // 如果index小于0,则在头部插入节点 void addAtIndex(int index, int val) { 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++; } // 删除第index个节点,如果index 大于等于链表的长度,直接return,注意index是从0开始的 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; //delete命令指示释放了tmp指针原本所指的那部分内存, //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后, //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针 //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间 tmp=nullptr; _size--; } // 打印链表 void printLinkedList() { 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); */
/** * 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* reverseList(ListNode* head) { ListNode* tmp; ListNode* pre = NULL; ListNode* cur = head; while(cur){ tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } return pre; } };
标签:tmp,index,head,cur,val,day03,next,链表,part01 From: https://www.cnblogs.com/zqh2023/p/17628618.html