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 kthen 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.
/**
* 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) {
if(k == 1) return head; ListNode cur = head;
ListNode curHead = head; //head of current subgroup
ListNode nextHead = head; //head of next subgroup
ListNode preTail = null; //tail of previous subgroup
while(true){
//check whether there's enough elements in subgroup
for(int i = 1; i < k; i++){
if(cur == null) break; //not enough element in subgroup
cur = cur.next;
}
if(cur == null) break; //not enough element in subgroup //reverse nodes
cur = curHead.next;
for(int i = 1; i < k; i++){
nextHead = cur.next;
if(preTail==null) {
cur.next = head;
head = cur;
}
else {
cur.next = preTail.next;
preTail.next = cur;
}
cur = nextHead;
}
curHead.next = nextHead;
preTail = curHead;
curHead = nextHead; }
return head;
}
}

因为while语句是无条件循环,特别要注意k==1时的死循环。

25. Reverse Nodes in k-Group (JAVA)的更多相关文章

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

  2. 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 算法思想:基本操作就是链表 ...

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

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

  4. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  5. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

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

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

  8. 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 ...

  9. 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

随机推荐

  1. PTA币值转化

    include<stdio.h> include<math.h> int n,s=0; int main(void){ int i,j,k,p=1,flag=1; int f( ...

  2. 了解原型设计工具pencil project

    Pencil Pencil是一个功能强大的界面原型设计工具,可以设计网页和桌面程序界面,侧重点在于设计,支持自定义控件和导出模板,功能确实很强大. 内置形状集合 从2.0.2开始铅笔默认包含更多形状集 ...

  3. 在线学习在CTR上应用的综述

    参考:https://mp.weixin.qq.com/s/p10_OVVmlcc1dGHNsYMQwA 在线学习只是一个机器学习的范式(paradigm),并不局限于特定的问题,模型或者算法. 架构 ...

  4. logging模块初识

    日志级别 import logging logging.debug("debug message")logging.info("info message")lo ...

  5. Kubernetes及Dashboard详细安装配置(Ubuntu14.04)

    前些日子部门计划搞并行开发,需要对开发及测试环境进行隔离,所以打算用kubernetes对docker容器进行版本管理,搭建了下Kubernetes集群,过程如下: 本流程使用了阿里云加速器,配置流程 ...

  6. sql_demo

    SELECT M.INSTNCODE, M.METHODCODE, M.CCYPAIRCODE, M.DIR, M.PRD, M.EXCHANGERATE, M.NEARRSKAMOUNT, M.TR ...

  7. 逆向工程vgenerator(一)

    前言 想要自己实现一个mybatis-generator类似的轮子,目前只实现MySQL部分的方法.利用下班时间,写了一个小项目,实现了这个功能.我准备分成三篇博客来写这个东西. 基类 /** *基类 ...

  8. java-索引

    集合 集合之深入理解HashMap HashMap的实现原理,以及在JDK1.7和1.8的区别 Java集合---ConcurrentHashMap原理分析 ConcurrentHashMap原理分析 ...

  9. OOP AOP

    OOP 一切皆对象,,,对象交互---功能,,,功能叠加---模块,,,模块叠加----系统 AOP   面向切面, 业务逻辑外,添加公共逻辑,增加日志功能,权限控制功能,缓存处理,异常处理,事务,性 ...

  10. PostgreSQL函数(存储过程)----笔记

    PostgreSQL 函数也称为 PostgreSQL 存储过程. PostgreSQL 函数或存储过程是存储在数据库服务器上并可以使用SQL界面调用的一组SQL和过程语句(声明,分配,循环,控制流程 ...