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

【牛客刷题】反转固定区间链表、每k个节点一组反转

 链表内指定区间反转_牛客题霸_牛客网

 

    ListNode* reverseList(ListNode* head, ListNode* tail) {ListNode* pre = nullptr;ListNode* cur = head;while (cur != tail) { 最后cur就是tailListNode* temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;}ListNode* reverseBetween(ListNode* head, int m, int n) {ListNode* dummyNode = new ListNode(0);dummyNode->next = head;ListNode* pre = dummyNode;for (int i = 1; i < m; i++) {pre = pre->next;}ListNode* start = pre->next;ListNode* end = pre->next;for (int i = m; i <= n; i++) {end = end->next;}pre->next = reverseList(start, end);//执行这个函数以后start变成传过去的链表的尾节点,所以直接连接start和end就行start->next = end;return dummyNode->next;}

链表中的节点每k个一组翻转_牛客题霸_牛客网 

    ListNode* reverseList(ListNode* head, ListNode* tail) {ListNode* pre = nullptr;ListNode* cur = head;while (cur != tail) {ListNode* temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;}ListNode* reverseKGroup(ListNode* head, int k) {// write code hereif (k == 1 || head->next == nullptr || head == nullptr) return head;ListNode* dummy = new ListNode(0);dummy->next = head;ListNode* pre = dummy;ListNode* start = pre->next;ListNode* end = pre;while (end->next != nullptr && end != nullptr) {start = pre->next;end = pre;int i;for (i = 0; i <= k; i++) {if (end->next) end = end->next;else {end = nullptr;break;}}if (i < k) break;else {pre->next = reverseList(start, end);start->next = end;pre = start;}}return dummy->next;}

http://www.lryc.cn/news/161782.html

相关文章:

  • 算法:数组常见套路1---双指针、取模、打擂台法
  • App 出海实践:Google Play 结算系统
  • 国际慈善日 | 追寻大爱无疆,拓世科技集团的公益之路
  • 关于DNS的一些认识
  • 游戏性能优化
  • 公开游戏、基于有向图的游戏
  • CSS学习笔记05
  • Linux查看指定端口是否被占用
  • 【Python 自动化】小说推文一键生成思路概述
  • MySQL中的字符集与排序规则详解
  • Java中如何进行加锁??
  • Pytorch3D多角度渲染.obj模型
  • MyBatisPlus 基础Mapperr接口:增删改查
  • 计算机网络与技术——概述
  • 详解TCP/IP协议第三篇:通信数据在OSI通信模型的上下传输
  • 《C++ primer plus》精炼(OOP部分)——对象和类(2)
  • 一点感受
  • VirtualBox RockyLinux9 网络连接
  • java 实现适配器模式
  • 后端常用的Linux命令大全
  • C++面向对象
  • 什么是栈顶缓存技术
  • TDesign的input标签
  • 从零开始学习 Java:简单易懂的入门指南之Map集合(二十三)
  • SpringBoot 拦截org.thymeleaf.exceptions.TemplateInputException异常
  • Qt之随机数
  • UWB学习——day2
  • 使用 multiprocessing 多进程处理批量数据
  • React 与 TS 结合使用时组件传参总结
  • 性能炸裂c++20协程+iocp/epoll,超轻量高性能异步库开发实战