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 个节点一组进行翻转,最后返 ...
随机推荐
- BUAA2020软工作业(四)——结对项目
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 结对项目作业 我在这个课程的目标是 进一步提高自己的编码能力,工程能力,团队协作能力 这个作业在哪 ...
- logstash的mutate过滤器的使用
logstash的mutate过滤器的使用 一.背景 二.需求 三.实现步骤 1.安装 `csv codec` 插件 2.准备需要读取的文件数据 3.编写 pipeline ,读取和输出数据 4.mu ...
- 【学习笔记】Vizing 定理
图染色问题的经典结论 定义 称一个边染色方案合法当且仅当每个顶点连出的所有边的颜色都互不相同,如果此时出现了 \(k\) 个颜色那么称该方案是图的一组 \(k\) 染色 一张无向图的边着色数为最小的 ...
- JavaScript中的this对象指向理解
在JavaScript中,this不是固定不变的,它的指向取决于上下文环境,一般的,认为this指向使用它时所在的对象.主要有以下几类指向: 在方法中,this 表示该方法所属的对象. 如果单独使用, ...
- 就因为把int改成Integer,第2天被辞了
本文节选自<设计模式就该这样学>之享元模式(Flyweight Pattern) 1 故事背景 一个程序员就因为改了生产环境上的一个方法参数,把int型改成了Integer类型,因为涉及到 ...
- NOIP模拟88(多校21)
前言 对于这套题的总体感觉就是难,然后就是自己很菜... 对于 T1 考试时只会一个最垃圾的背包,考完之后对于思路这一块也不是很顺利,大概这就是薄弱的地方吧. 然后 T2 是比较简单的一道题了,但是考 ...
- 面试官:能手写实现call、apply、bind吗?
1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...
- 正则表达式之grep
grep 的五个参数,基本的常用的: -a :将 binary 档案以 text 档案的方式搜寻数据 -c :计算找到 '搜寻字符串' 的次数 -i :忽略大小写的不同,所以大小写视为相同 -n :顺 ...
- Centos 系统常用编译环境
centos编译环境配置 yum install -y autoconf make automake gcc gcc-c++
- Part 18 $http service in AngularJS
In Angular there are several built in services. $http service is one of them. In this video, we will ...