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

news/2025/2/14 0:58:45/

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

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.ppmy.cn/news/1362275.html

相关文章

整理ArrayList和LinkedList中的方法

ArrayList 和 LinkedList 是 Java 中两种常用的列表&#xff08;List&#xff09;实现。它们提供了许多相同的方法&#xff0c;但由于内部实现的不同&#xff0c;这些方法的性能可能会有所不同。以下是一些常用的方法&#xff1a; 添加元素 add(E e): 在列表的末尾添加一个元…

面试经典150题【21-30】

文章目录 面试经典150题【21-30】6.Z字形变换28.找出字符串中第一个匹配项的下标68.文本左右对齐392.判断子序列167.两数之和11.盛最多水的容器15.三数之和209.长度最小的子数组3.无重复字符的最长子串30.串联所有单词的子串 面试经典150题【21-30】 6.Z字形变换 对于“LEETC…

前端工程化面试题 | 16.精选前端工程化高频面试题

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

【无标题】周总结、简单回顾下这周的工作进度

总结 1.完成产品列举场景所有时区功能的改造 2.完成依赖任务的开发 3.完成报表按照格式导出的时间数据改造 2024/2/25 阴 嘶~不冷 “上海申请加入下雪群聊” 上海破天荒的下了雪&#xff0c;毛毛细雪也是雪 我说的&#xff01; 虽然天气不佳&#xff0c;但好在没…

2.25基础会计学

资本公积是指由股东投入、但不能构成“股本”或“实收资本”的资金部分。 盈余公积是指公司按照规定从净利润中提取的各种积累资金。 所以区别在于盈余公积来自净利润。 借贷其实就是钱从哪来和到哪去的问题&#xff0c;来源是贷&#xff0c;流向是借。比如购入9w原材料&…

总结vue中的Router基本配置命令

Vue的Router是一个用于实现页面跳转和路由管理的插件。它可以帮助我们根据不同的URL请求加载不同的组件&#xff0c;以及实现前端路由功能。在使用Vue的Router时&#xff0c;需要对它进行基本配置。以下是Vue的Router基本配置命令。 1&#xff0c;安装Vue Router 使用npm安装V…

[回溯]组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target &#xff0c;找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 &#xff0c;并以列表形式返回。你可以按 任意顺序 返回这些组合。 candidates 中的 同一个 数字可以 无限制重复被选取 。如…

数据结构-数组

一,数组基础及注意事项 1,用来储存一组相同的类型的数据. 2,在内存中,分配连续的空姐,数组创建时要指定容量(大小). 3,创建格式: 数据类型 []数组名 int[] arr new int[10] int[] arr2 {1,2,3,4}. 4,索引--访问数组时通过索引进行操作. (注意:一定要理解索引的含义,在数据结…