[LeetCode 题解]: Reverse Nodes in K-Groups
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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
2. 题意
给定一个链表,将链表按照顺序分成k元素的组,并将每个组中的元素倒置。
注意: 当小组中的元素不满k个时,不进行倒置操作。
3. 思路
可以利用插入法进行链表倒置,采用递归方法。
(1)链表倒置,可以参考文章:
(2)将链表分成k元素小组。
设置一个指针pre,指向插入点。
当插入的元素满足k个后,进行递归。
例如:链表为 1->2->3->4->5 k=2
可以将小组分为 1->2 3->4 5
for i range from 1 to k
tmp <- head
#将tmp插入到pre之后
head = head->next
(3)递归
上述步骤完成时, head->val = 3,进入到下一次处理
(4)终止条件, 剩余的组中元素不足k个,将该组直接返回,不做倒置。
4: 解法
class Solution {
public:
int getListLen(ListNode *head){
int len=0;
ListNode *tmp=head;
while(tmp){len++;tmp=tmp->next;}
return len;
}
ListNode *reverseKGroup(ListNode *head, int k) {
if(k<=1|| !head || !head->next) return head;
int len=getListLen(head);
if(len<k) return head;
ListNode *pre=new ListNode(0);
ListNode *change=head;
for(int j=1;j<=k;j++){
ListNode *tmp = change;
change = change->next;
tmp->next = pre->next;
pre->next=tmp;
}
head->next=reverseKGroup(change,k);
return pre->next;
}
};
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3896010.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Reverse Nodes in K-Groups的更多相关文章
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- 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 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- 【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 ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode 025 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 an ...
- 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 ...
- [LeetCode] 25. 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. k ...
随机推荐
- Java 获取字符串长度 length()
Java 手册 实例: public class Length { public static void main(String[] args) { String str = "hgdfas ...
- [转]MVC 经验总结_EF
&& o.Name != "") .OrderByDescending(o => o.ID) .OrderBy(o => o.Name) .Select ...
- 在eclipse中使用mybatis-generator自动创建代码
1.eclipse中安装插件,地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/ ...
- web前端知识体系大全【欢迎补充】
大约在几个月之前,让我看完了<webkit技术内幕>这本书的时候,突然有了一个想法.想把整个web前端开发所需要的知识都之中在一个视图中,形成一个完整的web前端知识体系,目的是想要颠覆人 ...
- 使用GridFsTemplate在Mongo中存取文件
Maven依赖(还有一些springboot需要的) <parent> <groupId>org.springframework.boot</groupId> ...
- predict.glm -> which class does it predict?
Jul 10, 2009; 10:46pm predict.glm -> which class does it predict? 2 posts Hi, I have a question a ...
- Hadoop详细安装配置过程
步骤一:基础环境搭建 1.下载并安装ubuntukylin-15.10-desktop-amd64.iso 2.安装ssh sudo apt-get install openssh-server op ...
- Apache Kylin本地启动
首先:kylin是一种Online Analytics Platform. kylin 在Apache的首页是http://kylin.apache.org/cn/. kylin git代 ...
- latex如何插入空白行
1.~\\:一行空白2.\\[行距]:可加入任意间距的空白行 [xpt]
- 词项邻近 & 停用词 & 词干还原
[词项邻近] 邻近操作符(proximity)用于指定查询中的两个词项应该在文档中互相靠近,靠近程度通常采用两者之间的词的个数或者是否同在某个结构单元(如句 子或段落)中出现来衡量. [停用词] 一些 ...
