25. Reverse Nodes in k-Group (JAVA)
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)的更多相关文章
- [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 算法思想:基本操作就是链表 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 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 ...
- 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 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. ...
- 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. 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 ...
随机推荐
- Springboot+ActiveMQ(ActiveMQ消息持久化,保证JMS的可靠性,消费者幂等性)
ActiveMQ 持久化设置: 在redis中提供了两种持久化机制:RDB和AOF 两种持久化方式,避免redis宕机以后,能数据恢复,所以持久化的功能 对高可用程序来说 很重要. 同样在Active ...
- Listen and Write 18th Feb 2019
Weighted blanket has becomes very popular in many homes. they claim it can provide better sleep and ...
- HTML+CSS 对于英文单词强制换行但不截断单词的解决办法
如何处理长的单词和链接(强制换行,连接符,省略号等) 我们在前端开发中经常会遇到一些很长的文本串从它的容器中溢出,例如: 通过这样一段css可以有效解决这种问题: .dont-break-out { ...
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
总结: scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完 ...
- SpringMVC Controller接收前台ajax的GET或POST请求返回各种参数
这几天写新项目遇到这个问题,看这位博主总结得不错,懒得写了,直接转!原文:http://blog.csdn.net/yixiaoping/article/details/45281721原文有些小错误 ...
- 关于对CSS中超链接那部分的设置
a:link{ //正常下的超链接 color:red; //超链接的颜色 text ...
- [java,2017-05-17] 数据型参数趣谈
int的最大值是多少?加一呢?乘2呢? 第一个问题我想大多数人都知道,不知道后两个有多少人研究过. 首先上一段代码: public class DecimalTest { public static ...
- Oracle参数Arraysize设置对于逻辑读的影响分析
说明: 当执行一条SQL查询的时候,为了获得满足的数据,查询在这个过程中完成解析,绑定,执行和提取数据等一系列步骤,这些步骤都是单独执行的,满足条件的数据行必须由数据库返回给应用:对于任何大小的结果集 ...
- IntelliJ IDEA 设置背景图片
1.在idea中 按快捷键 Ctrl+Shift+A 出现如图 输入 Set Background Image 双击 Set Background Image 2.选择要添加的图片的路径 ...
- PHP截取特定字符串前后
$email = '13366540193@163.com' ;$domain = strstr ( $email , '@' );echo $domain ; // 打印 @163. ...