第二章 链表part01
链表理论基础
建议:了解一下链接基础,以及链表和数组的区别
文章链接:代码随想录
203.移除链表元素
建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。
题目链接/文章讲解/视频讲解::代码随想录
707.设计链表
建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点
题目链接/文章讲解/视频讲解:代码随想录
struct ListNode{ int val; ListNode *next; // ListNode():val(0),next(NULL){}; ListNode(int x) : val(x), next(NULL) {}; }; class myListNode{ private: struct ListNode* head; int size; public: myListNode(){ size = 0; head = new ListNode(-1); } void printList(){ ListNode* cur = head; while(cur != nullptr){ printf("%d->",cur->val); cur = cur->next; } printf("null\n"); } int get(int index){ if(index <0 || index >= size) return -1; ListNode* cur = head->next; for(int i=0;i<index;i++){ cur = cur->next; } return cur->val; } void addAtHead(int val){ addAtIndex(0,val); } void addAtTail(int val){ addAtIndex(size,val); } void addAtIndex(int index,int val){ if(index <0 || index > size ) return; ListNode* cur = head; for(int i=0;i<index;i++){ cur = cur->next; } ListNode* newNode= new ListNode(val); newNode->next = cur->next; cur->next = newNode; size++; } void deleteAtIndex(int index){ if(index <0 || index > size) return; ListNode* cur = head; for(int i = 0;i<index;i++){ cur = cur->next; } ListNode* p = cur->next; cur->next = p->next->next; delete p; size--; } }; int main(){ myListNode head; head.addAtIndex(0,1); head.addAtIndex(1,2); head.addAtIndex(2,3); head.deleteAtIndex(1); // head.addAtIndex(1,2); // head.addAtTail(3); //head.addAtIndex(1,4); head.printList(); }
206.反转链表
建议先看我的视频讲解,视频讲解中对 反转链表需要注意的点讲的很清晰了,看完之后大家的疑惑基本都解决了。
题目链接/文章讲解/视频讲解:代码随想录
文章链接:代码随想录
struct ListNode{ int val; ListNode *next; ListNode(int x): val(x),next(NULL){} }; ListNode* removeElements(ListNode* head, int val){ while(head != NULL && head -> val == val){ head = head -> next; } ListNode* cur = head; while(cur != NULL && cur->next != NULL){ if(cur->next->val == val){ ListNode* tmp = cur->next; cur-> next = cur->next->next; }else{ cur = cur->next; } } return head; } ListNode* removeElements_1(ListNode* head, int val){ ListNode *pre = new ListNode(0); pre->next = head; ListNode* cur =pre; while(cur->next != NULL){ if(cur->next->val == val){ ListNode* temp = cur->next; cur->next = temp->next; delete temp; }else{ cur = cur->next; } } head = pre->next; delete pre; return head; }; int main(){ ListNode* head = new ListNode(0); head->val = 1; ListNode* n2 = new ListNode(2); head -> next = n2; ListNode* n3 = new ListNode(6); n2 -> next = n3; ListNode* n4= new ListNode(3); n3 -> next = n4; ListNode* n5 = new ListNode(6); n4 -> next = n5; ListNode *ret = new ListNode(0); ret -> val = head->val; ret -> next = head->next; while(ret){ printf("%d\n",ret->val); ret = ret->next; } // removeElements(head,6); removeElements_1(head,6); printf("\nThis is new listnode\n"); while(head){ printf("%d\n",head->val); head = head->next; } }
struct ListNode{ int val; ListNode* next; ListNode(int x):val(x),next(nullptr){}; }; ListNode* reveseList(ListNode* head){ ListNode* cur = NULL; ListNode* pre = head; while(pre != NULL){ ListNode* p = pre->next; pre->next = cur; pre = cur; cur = p; } return pre; }