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

快速网站搭建百度推广介绍

快速网站搭建,百度推广介绍,网站开发课程的心得,网站建设设计语言LeetCode题目链接 https://leetcode.cn/problems/remove-linked-list-elements/ https://leetcode.cn/problems/design-linked-list/ https://leetcode.cn/problems/reverse-linked-list/ 题解 203.移除链表元素 重要的是创立头结点,这点在写题前已经经受过提示。 注…

LeetCode题目链接
https://leetcode.cn/problems/remove-linked-list-elements/
https://leetcode.cn/problems/design-linked-list/
https://leetcode.cn/problems/reverse-linked-list/

题解
203.移除链表元素
重要的是创立头结点,这点在写题前已经经受过提示。
注意移除代码中pre结点创建后,在循环中会跟着head往下走,因此要再创建一个cur结点,并令其初值等于pre,而不是直接令其next等于head,因为在遍历过程中移动了next指针,头结点被删除后可能在cur的next里没被改变,因此要直接令其值等于pre,来改变其next。
其他的创建链表代码有借鉴AI的(实在是AI有提示,没办法)。

707.设计链表
借助AI的帮助下写了几行(不是很多),主要是在按下标添加结点时,index长度等于链表长度时调用了addAtTail(val),这点比较要注意,其他的只是模拟。

206.反转链表
在最后return环节那里,是AI给我提示的(没办法,AI太快了),脑中模拟一下就知道是pre返回,这回完全没看题解,都是凭之前的印象做出的,第一遍刷题真的有用!

代码

//203.移除链表元素
#include <iostream>
#include <vector>
using namespace std;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) {if(head == NULL) return NULL;ListNode* pre = new ListNode;pre->next = head;ListNode* cur = pre;while (head != NULL) {if (head->val == val) {pre->next = head->next;head = head->next;}else {pre = head;head = head->next;}}return cur->next;}
};ListNode* createList(vector<int> nums) {if (nums.size() == 0) return NULL;ListNode *pre = new ListNode;pre->next = NULL;ListNode* cur = pre;for (int i = 0;i < nums.size();i++) {cur->next = new ListNode(nums[i]);cur = cur->next;}cur->next = NULL;return pre->next;
}int main() {Solution s;vector<int> nums = {  };ListNode* head = createList(nums);int val = 1;ListNode* node = head;while (node != NULL) {printf("%d ", node->val);node = node->next;}printf("\n");ListNode* result = s.removeElements(head, val);node = result;while (node != NULL) {printf("%d ", node->val);node = node->next;}return 0;
}
//707.设计链表
#include <iostream>
using namespace std;class MyLinkedList {
public:struct ListNode {int val;ListNode* next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}};;MyLinkedList() {preHead = new ListNode();}int getListLen() {ListNode* head = preHead->next;int len = -1;while (head != NULL) {len++;head = head->next;}return len;}int get(int index) {int len = getListLen();if (index < 0 || index > len) return -1;int num = -1;ListNode* head = preHead->next;while (head != NULL) {num++;if (num == index) {return head->val;}head = head->next;}return -1;}void addAtHead(int val) {ListNode* node = new ListNode(val);node->next = preHead->next;preHead->next = node;}void addAtTail(int val) {ListNode* pre = preHead;ListNode* head = preHead->next;while (head != NULL) {pre = head;head = head->next;}ListNode* node = new ListNode(val);pre->next = node;}void addAtIndex(int index, int val) {int len = getListLen() + 1;if (index > len) return;if (index == len) {addAtTail(val); //妙啊return;}ListNode* pre = preHead;ListNode* head = preHead->next;int num = -1;while (num < index - 1) {num++;pre = head;head = head->next;}ListNode* node = new ListNode(val);node->next = pre->next;pre->next = node;}void deleteAtIndex(int index) {int len = getListLen();if (index < 0 || index > len) return;ListNode* pre = preHead;ListNode* head = preHead->next;int num = 0;while (num < index) {pre = head;head = head->next;num++;}pre->next = head->next;delete head;}
private:ListNode* preHead;
};int main() {MyLinkedList myLinkedList;myLinkedList.addAtHead(1);myLinkedList.addAtTail(3);myLinkedList.addAtIndex(1, 2);    // 链表变为 1->2->3printf("%d\n", myLinkedList.get(1));              // 返回 2myLinkedList.deleteAtIndex(1);    // 现在,链表变为 1->3printf("%d\n", myLinkedList.get(1));              // 返回 3return 0;
}
//206.反转链表
#include <iostream>
#include <vector>
using namespace std;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) {if (head == NULL) return NULL;ListNode* pre = NULL;while (head != NULL) {ListNode* tmp = head->next;head->next = pre;pre = head;head = tmp;}return pre;}
};ListNode* createList(vector<int> nums) {if (nums.size() == 0) return NULL;ListNode* pre = new ListNode;pre->next = NULL;ListNode* cur = pre;for (int i = 0;i < nums.size();i++) {cur->next = new ListNode(nums[i]);cur = cur->next;}cur->next = NULL;return pre->next;
}int main() {vector<int> nums = {  };ListNode* head = createList(nums);ListNode* node = head;while (node != NULL) {printf("%d ", node->val);node = node->next;}printf("\n");Solution s;ListNode* result = s.reverseList(head);node = result;while (node != NULL) {printf("%d ", node->val);node = node->next;}return 0;
}
http://www.dtcms.com/a/586043.html

相关文章:

  • 婚纱摄影网站源码自己制作app的应用程序
  • 【经验分享】Spring Authorization Server 实现详解:构建企业级OAuth2认证中心(一)
  • 北京电脑培训网站备案号查询网站网址
  • 建可收费的网站一个网站策划需要多少钱
  • 吴恩达新课程:Agentic AI(笔记7)
  • 记录一次给Dell 10代cpu 重装系统遇到的BitLocker锁相关问题处理
  • Arbess CICD实践(2) - 使用Arbess+GitLab+PostIn实现Go项目构建/主机部署及接口自动化测试
  • 家具用品:撑起家的骨架与温度
  • 响应式网站建设的未来发展6滕州网站建设助企网络
  • 仿网站模板乐清网站制作公司招聘
  • .NET异步编程中内存泄漏的终极解决方案
  • 精读《JavaScript 高级程序设计 第4版》第14章 DOM(一)
  • Tr0ll 1 (VulnHub)做题笔记
  • 南宁建设银行官网招聘网站建设集团是做什么的
  • 【瑞芯微】【rk3128】【移植 qt 5.12.9】
  • 第十章 VLAN间通信
  • 苹果公司基于Transformer架构的蛋白质折叠开源模型SimpleFold-安装体验与比较
  • 网站制作的一般步骤中国建设人才网络学院
  • 长沙哪家做网站设计好网站设计制作的服务和质量
  • ENSP Pro Lab笔记:配置STP/RSTP/MSTP(5)
  • html5网站检测网站标签是什么
  • Java 内存模型(JMM)与 volatile、synchronized 可见性原理
  • 怎么开网站合肥做app公司
  • git本地分支创建
  • 操作系统(11)进程描述与控制--5种IO状态(1)
  • 整体设计 全面梳理复盘 之23 九宫格框架搭建与一体化开发环境设计 编程 之2
  • xm-软件测试工程师面经准备
  • python3.13 windows控制台使用python命令返回空行,python 命令执行失败,cmd python命令执行不显示
  • 电子商务论文8000字公司网站关键词优化
  • 广州网站设计制作报价建立自己的网站需要多少钱