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 ...
随机推荐
- spring 事物不回滚
使用spring控制事物,为什么有些情况事物,事物不回滚呢?? 默认spring事务只在发生未被捕获的 RuntimeException时才回滚. spring aop 异常捕获原理: 被拦截的 ...
- goland scope pattern 设置
有时候想要排除掉所有的 _test.go 文件去看工程,做如下设置: pattern里面填写: !file[*]:*_test.go
- (转)SQLServer分区表操作
原文地址:https://www.cnblogs.com/libingql/p/4087598.html 1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一 ...
- 好用的treeGrid
jquery easyui 官网:http://www.jeasyui.net/plugins/186.html 下面以学校班级情况,先贴出效果图吧! 数据库设计:红色框中为必须要有的列,右边三个为 ...
- Spring Boot 初识
发展到今天,spring已经是一个大家族了,如果想要使用其中的两到三个组件就会有多复杂的配置,有时候还有会版本不一致的错误,让人很无奈.于是,就有了spring Boot,spring Boot ...
- hadoop的环境变量
# hadoop && yarn export HADOOP_PREFIX=/home/ochadoop/apps/hadoop export HADOOP_HOME=${HADOOP ...
- pymysql -转自https://www.cnblogs.com/chenhaiming/p/9883349.html#undefined
PyMysql的几个重要方法 connect函数:连接数据库,根据连接的数据库类型不同,该函数的参数也不相同.connect函数返回Connection对象. cursor方法:获取操作数据库的Cur ...
- String(Java版本)
import java.io.UnsupportedEncodingException; public class Driver { public static void main(String[] ...
- VirtualBox安装Ubuntu16.04过程
1. 软件版本 Windows: Win7/Win10 VirtualBox: VirtualBox-6.0.24-108355-Win Ubuntu: ubuntu-16.04-desktop-am ...
- K-Means算法:图片压缩
#读取实例图片# from sklearn.datasets import load_sample_image from sklearn.cluster import KMeans import ma ...