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

408算法题leetcode--第14天

92. 反转链表 II

  • 92. 反转链表 II
  • 思路:头插法
  • 时间:O(n);空间:O(1)
/*** 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* reverseBetween(ListNode* head, int left, int right) {// 头插法if(head == nullptr) return nullptr;ListNode* dummy_head = new ListNode(-1, head);ListNode* p = dummy_head;for(int i = 0; i < left - 1; i++){p = p->next;}ListNode* cur = p->next;ListNode* next = nullptr;for(int i = 0; i < right - left; i++){next = cur->next;cur->next = next->next;next->next = p->next;p->next = next;}return dummy_head->next;}
};

21. 合并两个有序链表

  • 21. 合并两个有序链表
  • 时间:O(n+m);空间:O(1)
/*** 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* mergeTwoLists(ListNode* list1, ListNode* list2) {if(list1 == nullptr) return list2;if(list2 == nullptr) return list1;ListNode* p = list1, *q = list2;ListNode* dummy_head = new ListNode(-1);ListNode* t = dummy_head;while(q && p){if(q->val <= p->val){t->next = q;q = q->next;} else {t->next = p;p = p->next;}t = t->next;}t->next = q == nullptr ? p : q;return dummy_head->next;}
};

24. 两两交换链表中的节点

  • 24. 两两交换链表中的节点
  • 思路:类似之前的头插法
  • 时间:O(n);空间:O(1)
/*** 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* swapPairs(ListNode* head) {if(head == nullptr) return head;ListNode* dummy_head = new ListNode(-1, head);ListNode* pre = dummy_head, *cur = dummy_head->next;ListNode* next = nullptr;while(cur && cur->next){next = cur->next;// 头插法cur->next = next->next;next->next = pre->next;pre->next = next;// 向后移动pre = cur;cur = cur->next;}return dummy_head->next;}
};

876. 链表的中间结点

  • 876. 链表的中间结点
  • 思路:快慢指针
  • 时间:O(n);空间:O(1)
/*** 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* middleNode(ListNode* head) {// 快慢指针:快指针走2步,慢指针走1步ListNode* fast = head, *slow = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;}return slow;}
};

http://www.mrgr.cn/news/35128.html

相关文章:

  • FastStone Capture屏幕长截图软件注册码
  • Paper 0 | Visual Instruction Tuning
  • 【PyCharm 安装与运用秘籍】Python 和 PyCharm 安装指引,看此篇保证学会(附带优质插件)。
  • 【秋招笔试题】多多排序
  • 基于GPU的Julia集应用程序
  • [产品管理-34]:什么是战略?什么是公司战略?什么是产品战略?什么是创新战略?什么是技术战略?什么是产品创新战略?
  • tauri开发软件中,使用tauri自带的api用浏览器打开指定的url链接
  • Spring Cloud 教程(一) | 认识Spring Cloud
  • iptables添加有线网卡与无线网卡桥接转发规则
  • Java语法-类和对象(上)
  • Ubuntu USB设备绑定
  • project generator 简单使用(二)之 CLion 与 AC6
  • top 使用技巧
  • 基于vue框架的刺梨销售管理系统pgl49(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • 大势智慧亮相“第十届博博会”,展现数字文旅新质生产力!
  • React 中实现 vue keep-alive 功能的方法
  • web群集--rocky9.2部署zabbix服务端的详细过程
  • 如何使用ECharts制作折线图
  • 用于体积医学图像分割的跨视角差异依赖网络|文献速递--基于多模态-半监督深度学习的病理学诊断与病灶分割
  • 软件验收测试报告有什么作用?第三方验收测试报告包括哪些内容?