Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

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

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

题目要求我们把给出的链表按K个一组翻转,不足K个的不翻转

这个可以看做是上一个题目两两翻转节点的扩展,我们还是用递归来操作,先来确定下大致的处理过程

1.先从链表头开始数k个节点,记录个数,不满k个的按k个处理

2.判断上一步数出的节点个数,小于k则说明不用翻转,直接返回head就行

3.节点个数等于k,说明需要反转,利用3指针翻转链表的做法,把这个k节点链表翻转

4.前面几步把k个节点翻转了,剩下的节点递归调用该方法,然后返回翻转后链表的头结点

浓缩一下就是,先翻转k个节点,然后通过递归把k个节点之后的链表也翻转

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode prev = null;
ListNode cur = head;
ListNode next = null;
ListNode check = head;
int canProceed = 0;
int count = 0;
// 检查链表长度是否满足翻转
while (canProceed < k && check != null) {
check = check.next;
canProceed++;
}
// 满足条件,进行翻转
if (canProceed == k) {
while (count < k && cur != null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
count++;
}
if (next != null) {
// head 为链表翻转后的尾节点
head.next = reverseKGroup(next, k);
}
// prev 为链表翻转后的头结点
return prev;
} else {
// 不满住翻转条件,直接返回 head 即可
return head;
}
}
}

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表的更多相关文章

  1. [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  ...

  2. [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  ...

  3. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  4. 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划分 ...

  5. 蜗牛慢慢爬 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. ...

  6. 25. Reverse Nodes in k-Group[H]k个一组翻转链表

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. [leetcode 25]Reverse Nodes in k-Group

    1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  8. Java [leetcode 25]Reverse Nodes in k-Group

    题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  9. [LeetCode] 25. 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. k  ...

随机推荐

  1. 【Spring Boot-技巧】API返回值去除为NULL的字段

    简介 在前后端分离的微服务时代,后端API需要良好的规范.本篇主要将一个数据返回时的一个小技巧-- 过滤为空字段 解决痛点:将有效解决数据传输过程中的流量浪费. 组件简介 Jackson Object ...

  2. loj2289 [THUWC 2017]在美妙的数学王国中畅游(LCT+Taylor展开)

    link 题目大意: 你需要维护一个树 每个点都有个sin(ax+b)或exp(ax+b)或ax+b 你需要维护一些操作:连边.删边.修改某个点的初等函数.询问某条树链上所有函数带入某个值后权值和或不 ...

  3. 微信发送模版消息,PHP代码简单案例

    function http_request($url,$data=array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); c ...

  4. Services版本tomcat 内存和perm 设置

    当在tomcat的webapps文件夹中部署需要大量资源的war包时,启动tomcat,war包将不能被成功部署. tomcat安装有services版安装和开发版安装. 本经验将介绍如何设置两种版本 ...

  5. 浅谈C#数组(一)

    如果需要使用同一类型的多个对象,可以使用数组和集合(后面介绍).C#用特殊的记号声明,初始化和使用数组.Array类在后台发挥作用,它为数组中的元素排序和过滤提供了多个方法.使用枚举器,可以迭代数组中 ...

  6. C++ 构造函数与默认构造函数

    构造函数:C++用于构建类的新对象时需要调用的函数,该函数无返回类型!(注意:是“无”! 不是空!(void)). 默认构造函数:未提供显式初始值时,用来穿件对象的构造函数. 以上是二者的定义,但是单 ...

  7. SPOJ - COT2 离线路径统计

    题意:求\(u\)到\(v\)的最短路径的不同权值种类个数 树上莫队试水题,这一篇是上篇的弱化部分,但可测试以下结论的正确性 设\(S(u,v)\):\(u-v\)最短路径所覆盖的点集 \(S(u,v ...

  8. [转] 商业应用中Java浮点数的精确计算及表示

    [From] https://blog.csdn.net/stevene/article/details/586089 问题提出 (1).浮点数精确计算 胜利油田三流合一项目中一直存在一个问题,就是每 ...

  9. [转] Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch

    [From] http://www.tuicool.com/articles/JBvQrmj 本文讲解Spring Boot基础下,如何使用 ElasticSearch,实现全文搜索. 版本须知 sp ...

  10. flask综合案例

    一.项目准备 1.新建项目目录students,并创建虚拟环境 mkvirtualenv students 2.安装依赖环境 pip install flask==0.12.4 pip install ...