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

代码随想录算法训练营第三天

● 自己看到题目的第一想法

203.移除链表元素

方法一:

  1. 思路:
    设置虚拟头节点 dummyhead
    设置临时指针 cur 遍历 整个链表
    循环:
  • 如果 cur !=nullptr &&cur->next !=nullptr 则 遍历链表 否则结束遍历

  • 如果 cur->next == val 则 cur->next = cur->next->next

  • 如果 cur->next !=val 则 cur = cur->next

返回 return dummyhead->next

  1. 注意:用while循环
  2. 代码:
/*** 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) {ListNode* dummyhead = new ListNode(0);dummyhead->next = head;ListNode* cur = dummyhead;while(cur !=nullptr &&cur->next !=nullptr){if(cur->next->val == val){cur->next = cur->next->next;}else{cur = cur->next;}}head = dummyhead->next;delete dummyhead;return head;}
};
  1. 运行结果:
    在这里插入图片描述

方法二:

  1. 思路:
    直接在原链表上操作

    1.头节点是val值
    删除头节点 head = head->next;

    2.头节点不是val值
    定义一个临时变量cur 遍历整个链表
    循环 :

  • 如果cur !=nullptr && cur->next !=nullptr 则 遍历链表 否则结束遍历

  • 如果 cur->next == val 则 cur->next = cur->next->next

  • 如果 cur->next !=val 则 cur = cur->next

返回 return head;

  1. 注意:

  2. 代码:

/*** 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 = head->next;}ListNode *cur = head;while(cur !=nullptr && cur->next !=nullptr){if(cur->next->val == val ){cur->next = cur->next->next;}else{cur = cur->next;}}return head;}
};
  1. 运行结果:

在这里插入图片描述

707.设计链表

  1. 思路:
  2. 注意:
    cur应该指向_dummyhead 还是_dummyhead->next;
    链表的构造struct 还有 private中 链表的 定义
  3. 代码:
class MyLinkedList {
public:
struct ListNode{int val;ListNode* next ;ListNode(int val): val(val), next(nullptr){}
};MyLinkedList() {_size = 0;_dummyhead = new ListNode(0);}int get(int index) {if(index>(_size-1) || index<0){return -1;}ListNode* cur = _dummyhead;while(index){cur = cur->next;index--;}return cur->next->val;}void addAtHead(int val) {ListNode* newnode = new ListNode(val);newnode->next = _dummyhead->next;_dummyhead->next = newnode;_size++;}void addAtTail(int val) {ListNode* cur = _dummyhead;ListNode* newnode = new ListNode(val);while(cur !=nullptr && cur->next !=nullptr){cur =cur->next;}cur->next = newnode;_size++;}void addAtIndex(int index, int val) {ListNode* newnode = new ListNode(val);if(index<0)  index =0;if(index >_size) return ;ListNode * cur = _dummyhead;while(index--){cur = cur->next;}newnode->next = cur->next;cur->next = newnode;_size++;}void deleteAtIndex(int index) {if(index<0 || index>(_size-1)){return ;}ListNode*cur = _dummyhead;while(index--){cur = cur->next;}cur->next = cur->next->next;_size--;}private:int _size;ListNode* _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);*/
  1. 运行结果:
    在这里插入图片描述

206.反转链表

方法一:

  1. 思路:双指针
    定义pre= null, cur = head, 临时变量temp保存 cur->next;
    循环:

     cur != null让cur->next = pre;   pre = cur; cur = temp;
    

    返回:pre

  2. 注意:

  3. 代码:

/*** 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* cur = head;ListNode* pre = nullptr;ListNode* tmp ;while(cur !=nullptr){tmp = cur->next;cur->next = pre ;pre  =cur;cur = tmp;}return pre;}
};
  1. 运行结果
    在这里插入图片描述
    方法二:

  2. 思路:递归法:

    先完成翻转的第一步:
    确定终止条件: cur==null 返回 pre
    循环体: cur ->next = pre
    递归下去 return reverse(cur, tmp)

  3. 注意:

  4. 代码:

/*** 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* reverse(ListNode* pre, ListNode* cur ){if(cur == nullptr) return pre;ListNode* temp;temp = cur->next;cur->next = pre;return reverse(cur, temp);}ListNode* reverseList(ListNode* head) {return reverse(nullptr, head);}
};
  1. 运行结果:
    在这里插入图片描述
http://www.lryc.cn/news/307155.html

相关文章:

  • 蓝桥杯刷题1
  • 前端学习---- 前端HTML基本元素的介绍
  • 力扣思路题:丑数
  • C# this关键字的作用
  • Ubuntu18.04虚拟机磁盘扩容-lvm
  • 低代码开发:数字赋能智能制造的未来
  • janus-gateway的videoroom插件的RTP包录制功能源码详解
  • nginx+keepalived实现nginx高可用集群以及nginx实现Gateway网关服务集群
  • 主键、外键、建表范式、MySQL索引、用户管理
  • 探究前端路由hash和history的实现原理(包教包会)
  • 幻兽帕鲁服务器多少钱?有买过的吗?
  • MCU独立按键单控LED实现
  • [数据集][目标检测]游泳者溺水数据集VOC+YOLO格式2类别895张
  • 2402C++,C++使用单链列表
  • 《Docker极简教程》--Docker服务管理和监控--Docker服务的监控
  • C++初阶 | [八] (下) vector 模拟实现
  • 信息安全计划
  • 【更新完毕】2024牛客寒假算法基础集训营6 题解 | JorbanS
  • FL Studio All Plugins Edition2024中文完整版Win/Mac
  • 神经网络系列---归一化
  • 2023 龙蜥操作系统大会演讲实录:《兼容龙蜥的云原生大模型数据计算系统——πDataCS》
  • 【Vue渗透】Vue站点渗透思路
  • 主数据管理是数字化转型成功的基石——江淮汽车案例分享
  • 【Spring连载】使用Spring Data访问 MongoDB(十一)----加密Encryption (CSFLE)
  • 【postgresql】数据表id自增与python sqlachemy结合实例
  • 什么是索引?在 MySQL 中有哪些类型的索引?它们各自的优势和劣势是什么?
  • Docker安装与基础知识
  • 搭建Facebook直播网络对IP有要求吗?
  • Qt开发:MAC安装qt、qtcreate(配置桌面应用开发环境)
  • python学习网站