43-Reverse Nodes in k-Group
- Reverse Nodes in k-Group My Submissions QuestionEditorial Solution
Total Accepted: 58690 Total Submissions: 212820 Difficulty: Hard
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
当之无愧的难题,其实思路比较简明,但是要写出完全无bug的代码简直太难了,改了很多遍才改到无bug,因为里面的指针运算太多了,一不注意就掉坑了,改变了指针所向内容的next,确仍然引用
结果:
Submission Details
81 / 81 test cases passed.
Status: Accepted
Runtime: 24 ms
beats:41.82%
思路:找到每一段末尾标记,然后该段范围内进行逆转,同时保留逆转后的末尾元素。
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==NULL||head->next==NULL||k==1)return head;
ListNode *fir=head,*sec=head,*pre_fir=NULL,*pre_sec=NULL;
int finished=0;
int count=1;
while(!finished){
int i=1;
while(i++<k&&sec!=NULL){sec=sec->next;} //fir.....sec段总的K个元素
ListNode *sec_next=(sec==NULL)?NULL:sec->next;
if((i-1)==k&&(sec!=NULL)){ //注意,只有当找了k次且最后一次找到了(不为空)才进行这段元素原地逆转
ListNode *tmp = fir->next; //逆转时的当前元素
ListNode *prenode = fir; //逆转时保留的前一元素
while(tmp!=sec_next) //当前元素没到sec末尾元素,
{
ListNode *nextnode = (tmp->next); //保存当前元素的下一元素
tmp->next = prenode; //当前元素指向前一个元素
prenode = tmp; //前一元素右移
tmp = nextnode; //当前元素右移
}
fir->next = NULL; //第一段的首元素变成末尾元素。
if(pre_fir!=NULL){pre_fir->next = prenode;pre_fir = fir;}//pre_fir上一段逆转后的末尾指向当前的逆转后的开头
else {head = prenode;pre_fir=fir;}//针对第一次前一段的逆转前的为空
fir = sec_next; //开始下一段元素逆转,sec_next保存上一段sec的下一元素
sec = fir; //将sec置为fir一样的值,开始逆转流程。
}
else {
finished = 1;
if(pre_fir!=NULL)pre_fir->next = fir;
}
}
return head;
}
};
43-Reverse Nodes in k-Group的更多相关文章
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 25.Reverse Nodes in k-Group (List)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Leetcode 25/24 - Reverse Nodes in k-Group
题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...
随机推荐
- 轻量级 Java 基础开发框架,Solon & Solon Cloud 1.5.52 发布
Solon 已有120个生态扩展插件,此次更新主要为细节打磨: 插件 mybatis-solon-plugin 增加 mappers 单行配置支持 之前的多行模式: mybatis.db1: type ...
- qwt使用细节
在使用QWT进行二维曲线绘制,使用方法如下: class Plot: public QwtPlot { Q_OBJECT -- } 报错:error LNK2001: 无法解析的外部符号"p ...
- STM32采集AD的输入阻抗问题
在做一款消费电子产品时,需要采集电池电压(3.3V-4.2V),同时在休眠的时候希望尽量减小待机电流.电池电压采集电路采用两个1%的300K电阻进行分压,由该电路引起的待机电路为4.2/(300+30 ...
- 零基础入门非常好的C语言基础资料
C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { in ...
- 万维网www与HTTP协议
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105901440 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 数据治理之元数据管理的利器——Atlas入门宝典
随着数字化转型的工作推进,数据治理的工作已经被越来越多的公司提上了日程.作为Hadoop生态最紧密的元数据管理与发现工具,Atlas在其中扮演着重要的位置.但是其官方文档不是很丰富,也不够详细.所以整 ...
- MySQL实战优化之InnoDB整体架构
一.InnoDB 更新数据得整体架构 每个组件的作用说明: 用一条更新数据来说明每个主键得作用: update student set name = 'zhangsan' where id = 10 ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- cf 12B Correct Solution?(贪心)
题意: 一个数a,一个数b. 现在要将a的每一位上的数字重新整理,生成一个新的不含前导0的数a'. 问a'是否等于b. 思路: a上每一位的数字从小到大排序,找到最小的非零数和第一位交换. 代码: c ...
- PWN二进制漏洞学习指南
目录 PWN二进制漏洞学习指南 前言 前置技能 PWN概念 概述 发音 术语 PWN环境搭建 PWN知识学习途径 常见漏洞 安全机制 PWN技巧 PWN相关资源博客 Pwn菜鸡小分队 PWN二进制漏洞 ...