当前位置: 首页 > news >正文

代码随想录训练营day3:链表part1

理论

链表的增删操作时间复杂度O(1),查询时间复杂度O(n),因为要从头结点开始。使用场景和数据完全相反
链表的储存地址是不连续的。也和数组不同。

移除链表元素

利用虚拟头结点可以同意操作。不然删除头结点需要额外写。
记得返回的是虚拟头结点的next而不是虚拟头结点return dummyhead。哈哈哈

class Solution {
public:ListNode* removeElements(ListNode* head, int val) {ListNode* dummyhead = new ListNode(60);dummyhead->next=head;ListNode* cur=dummyhead;while(cur->next!=NULL){if(cur->next->val == val){ListNode* temp=cur->next;cur->next = cur->next->next;delete temp;}else{cur=cur->next;}}return dummyhead->next;}
};

设计链表

总是忘记判定插入或者删除的位置是否有效。

class MyLinkedList {
public:struct ListNode {int val;ListNode *next;ListNode(int val) : val(val), next(nullptr) {}};MyLinkedList() {dummyhead=new ListNode(0);size=0;}int get(int index) {if(index>size-1)return -1;ListNode* cur=dummyhead->next;for(int i=0;i<index;i++){cur=cur->next;}return cur->val;}void addAtHead(int val) {ListNode* head= new ListNode(val);head->next=dummyhead->next;dummyhead->next=head;size++;}void addAtTail(int val) {if(size==0) dummyhead->next=new ListNode(val);else{ListNode* cur=dummyhead->next;while(cur->next != NULL){cur=cur->next;}cur->next= new ListNode(val);}size++;}void addAtIndex(int index, int val) {if(index>size) return;ListNode* cur=dummyhead;for(int i=0;i<index;i++){cur=cur->next;}ListNode* temp=new ListNode(val);temp->next=cur->next;cur->next=temp;size++;}void deleteAtIndex(int index) {if(index>=size) return;ListNode* cur=dummyhead;for(int i=0;i<index;i++){cur=cur->next;};ListNode* temp=cur->next;cur->next=cur->next->next;delete temp;size--;}//void printLinkedList(){//}
private:int size;ListNode* dummyhead;
};

翻转链表

中间过程想到了用三个指针,双指针+储存临时下一个的指针。
但是开头和结尾的处理过程没想出来。
直接让pre=head,这样的话还得加上head->next=nullptr才表示一条链表结束了。
所以让pre=null就不用特殊处理开头和结尾了。

    ListNode* reverseList(ListNode* head) {//if(head->next==nullptr) return nullptr;//ListNode* dummyhead= new ListNode(0);//dummyhead->next=head;//ListNode* pre=head;//ListNode* cur=pre->next;//ListNode* next=cur->next;//cur->next=pre;//head=reversal(cur,next);//return head;if(head==nullptr) return nullptr;ListNode* pre=nullptr;ListNode* cur=head;while(cur!=nullptr){ListNode* next=cur->next;cur->next=pre;pre=cur;cur=next;}return pre;}

快忘记递归怎么写啦,就是递归套递归。

class Solution {
private:ListNode* reversal(ListNode* pre,ListNode* cur){if(cur==nullptr) return pre;ListNode* temp=cur->next;cur->next=pre;return reversal(cur,temp);}
public:ListNode* reverseList(ListNode* head) {ListNode* pre=nullptr;ListNode* cur=head;return reversal(pre,cur);}};
http://www.lryc.cn/news/209866.html

相关文章:

  • Bootstrap的咖啡网站实例代码阅读笔记
  • 2021年06月 Python(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • FileWriter文件字符输出流
  • Vue的八个基础命令及作用
  • Log日志详解分析
  • 【API篇】九、Flink的水位线
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • Java面试题-Redis-第一天(Redis简单介绍)
  • Java 生成和读取JSON文件
  • k8s-----26、细粒度权限管理 RBAC
  • 【Unity ShaderGraph】| 制作一个 高级流体水球效果
  • 日常软件游戏丢失msvcp120dll怎么修复?分享5个修复方法
  • 自动驾驶之—2D到3D升维
  • ubuntu18.4(后改为20.4)部署chatglm2并进行基于 P-Tuning v2 的微调
  • 爬虫-获取数据xpath
  • SpringBoot中使用JdbcTemplate访问Oracle数据库
  • 【Linux】权限完结
  • 计算机网络-应用层(3)
  • 虎去兔来(C++)
  • docker基础镜像定制
  • 解决git action定时任务执行失败的方法
  • Node编写重置用户密码接口
  • Day13力扣打卡
  • 独立开发者知识贴
  • 软考系列(系统架构师)- 2009年系统架构师软考案例分析考点
  • C语言每日一题(21)删除排序数组中的重复项
  • 如何快速解决d3dcompiler_43.dll缺失问题?五种方法快速解决
  • mongodb数据迁移的方法
  • Spring MVC 中文文档
  • RedissonCach的源码流程