当前位置:网站首页>【7.4】25. K 个一组翻转链表
【7.4】25. K 个一组翻转链表
2022-07-07 21:52:00 【howtoloveyou】
class Solution {
public:
//反转一个链表并返回头尾节点
pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
ListNode* prev = tail->next;
ListNode* p = head;
while (prev != tail) {
ListNode* nex = p->next;
p->next = prev;
prev = p;
p = nex;
}
return {
tail, head};
}
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* hair = new ListNode(0);
hair->next = head;
ListNode* pre = hair;
while (head) {
ListNode* tail = pre;
for (int i = 0; i < k; ++i) {
tail = tail->next;
if (!tail) {
return hair->next;
}
}
ListNode* nex = tail->next;
tie(head, tail) = myReverse(head, tail); //进行翻转并将head,tail赋予新的值
pre->next = head; //将链表的头尾进行链接
tail->next = nex;
pre = tail;
head = tail->next;
}
return hair->next;
}
};
边栏推荐
- Three questions TDM
- USB (XIV) 2022-04-12
- Lm12 rolling heikin Ashi double K-line filter
- Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
- Matlab SEIR infectious disease model prediction
- B / Qurt Utilisateur Guide (36)
- LeeCode -- 6. Zigzag transformation
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- CAIP2021 初赛VP
- php 使用阿里云存储
猜你喜欢

UE4_ Ue5 panoramic camera

B_QuRT_User_Guide(38)

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration

UE4_UE5结合罗技手柄(F710)使用记录

PCI-Express接口的PCB布线规则

三问TDM

Unity3d Learning Notes 6 - GPU instantiation (1)

建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置

USB (XV) 2022-04-14
随机推荐
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
HDU 4747 mex "recommended collection"
Cloud native is devouring everything. How should developers deal with it?
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
Turbo introder common scripts
VS扩展工具笔记
How can we make money by making video clips from our media?
Extended tree (I) - graphic analysis and C language implementation
Matlab SEIR infectious disease model prediction
leetcode-520. Detect capital letters -js
Illegal behavior analysis 1
Mysql索引优化实战二
Force deduction solution summary 648 word replacement
POJ2392 SpaceElevator [DP]
Deep understanding of MySQL lock and transaction isolation level
Unity3D学习笔记4——创建Mesh高级接口
USB (XVII) 2022-04-15
USB (XVI) 2022-04-28
[compilation principle] lexical analysis design and Implementation
List. How to achieve ascending and descending sort() 2020.8.6