[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 ...
随机推荐
- boost::threadpool 调用类成员变量并传入参数 的方法
1. 首先到官网下载 http://threadpool.sourceforge.net/ 2. 包含头文件 #include "../boost/threadpool.hpp& ...
- QT win 安装配置
QT windows 版安装配置 安装包:链接:https://pan.baidu.com/s/1LCj2V3xQ1wB9_7zmE5tV6Q 密码:bn9r 首先安装QT Creator 双击安装文 ...
- [Android] 开发第九天
以下代码完全使用代码来控制 UI 界面,不被推荐使用. package com.oazzz.test2; import android.graphics.LinearGradient; import ...
- jquery文件的引入
上节课说到,一般情况下,是库的文件,该库中都会抛出来构造函数或者对象 ,如果是构造函数,那么创建对象,如果是对象直接调用属性和方法 使用jquery第一步,先引入jquery,然后再写相应的jquer ...
- iis应用程序池假死问题
“Comprehensive orientate 16:05:43 查看原文 IIS貌似问题不少 问:IIS 网站 并发连接线不多,但是运行一段时间后 就非常慢,系统资源占用都正常,一回收应用 ...
- 让IE依据HTML头标签选择显示模式
文件兼容性用于定义让IE如何编译你的网页.此文件解释文件兼容性,如何指定你网站的文件兼容性模式以及如何判断一个网页该使用的文件模式. 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE ...
- oracle执行sql文件
oracle执行sql文件 在PL/SQL中直接用command window执行就可以了: PL/SQL developer----->File------>New---->com ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- Java Reference & ReferenceQueue一览
Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...
- unity animation readonly 无法加事件?
目前找到的解决方案是用代码加Event: using System.Collections; using System.Collections.Generic; using UnityEngine; ...