当前位置:网站首页>Nodes in the linked list are flipped in groups of k
Nodes in the linked list are flipped in groups of k
2022-08-10 22:30:00 【dry rice white】
描述
将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表
如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样
你不能更改节点中的值,只能更改节点本身.
数据范围: \ 0 \le n \le 2000 0≤n≤2000 , 1 \le k \le 20001≤k≤2000 ,链表中每个元素都满足 0 \le val \le 10000≤val≤1000
要求空间复杂度 O(1)O(1),时间复杂度 O(n)O(n)
例如:
给定的链表是 1\to2\to3\to4\to51→2→3→4→5
对于 k = 2k=2 , 你应该返回 2\to 1\to 4\to 3\to 52→1→4→3→5
对于 k = 3k=3 , 你应该返回 3\to2 \to1 \to 4\to 53→2→1→4→5
This topic has beenAC

解题思路:
拿到这题,One idea is to use the stack to write.As for why I thought of using a stack?Because it needs to be reversed,Here I see the requirements of the title,空间复杂度为 O(1).The first idea is to save the value,通过 newA new node has been implemented.但是仔细一想,It seems impossible,Because the space complexity is definitely not in line with this,突然灵机一动,We can use the stack to store nodes that are pointers,nice.
1→2→3→4→5 k = 2时
1→2→3→4→5 k = 3时
4和5It doesn't need to be flipped,So if you use a stack, it will bring another problem,Because the stack structure is first in last out,If you use the stack to save it will definitely lead to,4,5will also be flipped.
这里有两个解决方法,
1.把4,5再次入栈(入另一个栈),然后再输出 ok
2.Might as well not use the stack structure,We use a two-way queue(Moreover, the efficiency of inserting and deleting at the front and rear ends of the two-way queue is also quite high,So we use this structure best)
Let's look at an example:
1→2→3→4→5 k = 3时
准备工作,A pointer to this linked list head.一个deque,一个中间变量 tmpk
Some handy auxiliary pointers s和ans




Here we make a loop,while(head != nullptr) 是的,It can be done in one pass


此时退出循环,okk
判断 que是否为空或者tmpk是否等于 0
如果que不为空的话,说明queAt this time, the node does not need to be flipped
所以

那么这题就ok了,Of course the topic has some details,This can only be modified according to the actual test emptying
比如 {},2 {1,2},3 {1},2等
Attached the code I passed:
class Solution {
public:
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* reverseKGroup(ListNode* head, int k) {
// write code here
deque<ListNode*> sta;
int tmpk = k;
ListNode *s = nullptr;
ListNode *ans = nullptr;
while(head != nullptr)
{
sta.push_back(head);
head = head->next;
tmpk--;
if(tmpk == 0)
{
if(s == nullptr)
{
s = sta.back();
sta.pop_back();
ans = s;
}
while(!sta.empty())
{
s->next = sta.back();
sta.pop_back();
s = s->next;
}
tmpk = k;
}
}
while(!sta.empty())
{
if(s == nullptr)
{
s = sta.front();
sta.pop_front();
ans = s;
}
else
{
s->next = sta.front();
sta.pop_front();
s= s->next;
}
}
if(s != nullptr)
s->next = nullptr;
return ans;
}
};
边栏推荐
- shell (text printing tool awk)
- uni-app微信小程序——下拉多选框
- GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
- QT笔记——用VS + qt 生成dll 和 调用生成的dll
- xshell (sed command)
- 爬虫request.get()出现错误
- Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
- VLAN huawei 三种模式
- 翻译科技论文,俄译中怎样效果好
- String类的常用方法
猜你喜欢

unusual understanding

【开源教程5】疯壳·开源编队无人机-飞控固件烧写

An article to teach you a quick start and basic explanation of Pytest, be sure to read

FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能

Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology

VLAN huawei 三种模式

使用SylixOS虚拟串口,实现系统串口自由

从斐波那契 - 谈及动态规划 - 优化

c语言之 练习题1 大贤者福尔:魔法数,神奇的等式

Using SylixOS virtual serial port, serial port free implementation system
随机推荐
字节跳动原来这么容易就能进去...
STL-stack
Common interview questions for APP UI automation testing, maybe useful~
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
RK3399平台开发系列讲解(内核驱动外设篇)6.35、IAM20680陀螺仪介绍
Likou 221 questions, the largest square
LeetCode Daily Question (1573. Number of Ways to Split a String)
自组织是管理者和成员的双向奔赴
2022年8月10日:使用 ASP.NET Core 为初学者构建 Web 应用程序--使用 ASP.NET Core 创建 Web UI(没看懂需要再看一遍)
力扣215题,数组中的第K个最大元素
labelme - block drag and drop events
Black cats take you learn Makefile article 13: a Makefile collection compile problem
shell programming without interaction
链表中的节点每k个一组翻转
FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能
Black cat takes you to learn Makefile Part 12: Summary of common Makefile problems
【PCBA方案】电子握力测试仪方案she‘ji
ThreadLocal comprehensive analysis (1)
高数_复习_第5章:多元函数微分学
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function