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.
我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来。
public class ReverseNodes {
public static void main(String[] args) {
String str = "1->2->3->4->5->6->7->8->9->10->11->12->13->14->15";
String[] strArray = str.split("->");
int k = 2;
/**
* int k = 2; 2->1->4->3->6->5->8->7->10->9->12->11->14->13->15
* int k = 4; 4->3->2->1->8->7->6->5->12->11->10->9->13->14->15
* int k = 3; 3->2->1->6->5->4->9->8->7->12->11->10->15->14->13
*/
String[] results = reveseNodes(k,strArray);
for(int i = 0; i < results.length; ++i){
if(i == strArray.length - 1){
System.out.print(results[i]);
}else{
System.out.print(results[i] + "->");
}
}
}
public static String[] reveseNodes(int k, String[] strArray) {
int left = strArray.length % k;
int start = 0;
if(strArray.length / k == 0){
return strArray;
}
while(start < strArray.length - left){
DiedaiReverse(start,k,strArray);
start += k;
}
return strArray;
}
public static void DiedaiReverse(int start, int k, String[] strArray) {
int j = 0;
if(k%2 == 0){
for(int i = start; i < (start + start + k)/2; ++i){
String temp = strArray[i];
strArray[i] = strArray[start+k-1-j];
strArray[start+k-1-j] = temp;
++j;
}
}else{
for(int i = start; i <= (start + start + k)/2; ++i){
String temp = strArray[i];
strArray[i] = strArray[start+k-1-j];
strArray[start+k-1-j] = temp;
++j;
}
}
}
}
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.的更多相关文章
- 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
class Solution { public: ListNode *reverseKGroup(ListNode *head, int k) { if (!head || !(head->ne ...
- [Linked List]Reverse Nodes in k-Group
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
- Data Structure Linked List: Reverse a Linked List in groups of given size
http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...
随机推荐
- Maven中pom.xml的scope
一.compile:编译范围compile是默认的范围:如果没有提供一个范围,编译范围依赖在所有的classpath 中可用,同时它们也会被打包.而且这些dependency会传递到依赖的项目中. ...
- 浅谈WEB编辑器——HBuilder
我自己用过的WEB编辑器有两种:HBuilder和Dreamweaver.这两种编辑器各有各的特点,但是相对来说,我倾向于前者:后者给我的感觉就是功能繁杂,运行起来慢,而且编码的便捷度不高,时不时需要 ...
- 【Python3之异常处理】
一.错误和异常 1.错误 代码运行前的语法或者逻辑错误 语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) def test: ^ SyntaxError: inva ...
- Quartz使用-入门使用
注:这里使用的是Quartz1.6.5版本(包:quartz-1.6.5.jar) //测试main函数 //QuartzTest.java package quartzPackage; import ...
- MAC 相关操作解析
MAC 相关操作解析 OS 显示桌面 f11 F1~F12 fn + F1~F12 撤销重做 command + z command + shift + z 图片预览 选择图片 空格 上下左右 svn ...
- 把sql输出成。sql文件
作者原创,转载注明出处: 代码: package importfile; import java.io.*; import java.io.PrintWriter; import java.sql.C ...
- [基础架构]PeopleSoft Application Server 重要文件说明
我们都知道PeopleSoft是由几个不同的服务组成的,他们在PeopleSoft体系结构中扮演着自己的角色.这些服务具有不同的文件结构并包含重要的可执行文件和配置文件. 以下是Peoplesoft体 ...
- Java负载均衡-輪詢法
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; /* ...
- 新篇章之我的java学习之路下
昨天写下了人生的第一篇博客,今天接着写我的java学习之路有关开发及框架的学习过程. 想要学好java语言,只学习一些java的基本语法对实际开发中的用处还是不大的,所以我们还要掌握一些有关javaW ...
- 在Visual Studio 2017中发现的问题
最近抽时间学习了一下 C# 6.0 的新特性,其中的一个新特性是 ?. 运算符,请看下面的一个简单示例: 当我故意修改成错误代码的时候,请接着看下面的示例: 我想把代码修改成list?[0].Coun ...