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

反转链表 K个一组翻转链表

目录

LeetCode206 反转链表

LeetCode92 反转链表II

LeetCode25 K个一组翻转链表


LeetCode206 反转链表

/**
 * 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* pre=nullptr;
        ListNode* cur=head;
        while(cur){
            ListNode* nex=cur->next;
            cur->next=pre;
            pre=cur;
            cur=nex;
        }
        return pre;
    }
};

LeetCode92 反转链表II

/**
 * 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) {
        ListNode* dummy=new ListNode(0);
        dummy->next=head;
        ListNode* p=dummy;
        for(int i=0;i<left-1;i++) p=p->next;
        ListNode* cur=p->next;
        ListNode* pre=nullptr;
        for(int i=0;i<right-left+1;i++){
            ListNode* nex=cur->next;
            cur->next=pre;
            pre=cur;
            cur=nex;
        }
        p->next->next=cur;
        p->next=pre;
        return dummy->next;
    }
};

LeetCode25 K个一组翻转链表

 

/**
 * 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* reverseKGroup(ListNode* head, int k) {
        int n=0;
        for(ListNode* i=head;i;i=i->next) n++;
        ListNode* dummy=new ListNode(0);
        dummy->next=head;
        ListNode* p=dummy;
        ListNode* cur=head;
        for(;n>=k;n-=k){
            ListNode* pre=nullptr;
            for(int i=0;i<k;i++){
                ListNode* nex=cur->next;
                cur->next=pre;
                pre=cur;
                cur=nex;
            }
            ListNode* ne=p->next;
            p->next->next=cur;
            p->next=pre;
            p=ne;
        }
        return dummy->next;
    }
};


http://www.kler.cn/news/356712.html

相关文章:

  • Mac中安装以及配置adb环境
  • winUI3 c++ 入门 1、入门几个坑
  • Linux学习笔记9 文件系统的基础
  • 算法之二分查找法
  • IP地理位置定位系统之应用场景划分
  • 大厂面试真题-说一说rpc和http的区别?http能否代替kafka
  • ui自动化知识点-web端
  • 无向图中的一些问题与处理(上接无向图知识简记)
  • GitLab 老旧版本如何升级?
  • 玩转大模型(二)启动一个大模型
  • 怎样将pdf转换成ppt?关于几种PDF转PPT的方法介绍
  • web端使用高德地图逆地理编码
  • GOT-OCR-2-GUI - 一个强大的AI文本识别模型 OCR文字识别 图片文字识别 本地一键整合包下载
  • 【ROS2实操五】通信机制补充
  • 2024年诺贝尔物理学奖揭晓:AI背后的“造梦者”是谁?
  • Thread的基本用法
  • MySQL 中utfmb3和utfmb4字符集区别
  • 算法——python实现归并排序
  • 中小型企业网络的设计与实现
  • 基于语音识别的停车共享小程序(lw+演示+源码+运行)