我用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.的更多相关文章

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

  2. [Linked List]Reverse Nodes in k-Group

    Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...

  3. 【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 ...

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

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

  6. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

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

  8. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

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

随机推荐

  1. Android 7.0 安装器安装过程分析 (com.android.packageinstaller)

    1 安装入口PackageInstallerActivity,这个类只是在安装前做准备.通过各种校验,然后弹出被安装应用的权限框,等待用户安装.具体的流程如下 1.1  求mSessionId 如果是 ...

  2. Python爬虫从入门到放弃(十)之 关于深度优先和广度优先

    网站的树结构 深度优先算法和实现 广度优先算法和实现 网站的树结构 通过伯乐在线网站为例子: 并且我们通过访问伯乐在线也是可以发现,我们从任何一个子页面其实都是可以返回到首页,所以当我们爬取页面的数据 ...

  3. Mongodb基础用法及查询操作[转载]

    插入多条测试数据> for(i=1;i<=1000;i++){... db.blog.insert({"title":i,"content":&qu ...

  4. ci框架中输出sql语句

  5. Maven pom.xml配置详解

    POM的全称是“ProjectObjectModel(项目对象模型)”. 声明规范 <projectxmlns="http://maven.apache.org/POM/4.0.0&q ...

  6. webpack3中文版使用参考文档--全面解析webpack.config.js

    Webpack目前官方发布的最新版本是3.1.0,相对于2.0的怎么本,在语法上没有变动,只是新增了功能.使用webpack,需要事先安装node.js,并对node.js生态有一些基本的了解,比如( ...

  7. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  8. Eclipse 快捷键和模板设置

    快捷键设置 菜单  Window --> Preferences---General---Keys Content Assist:  代码提示快捷键 模板设置 新建一个模板 在Insert Va ...

  9. js字符串与数组的相互转换

    一.数组转字符串,通过join()拼接数组元素 var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); c = a.join('');consol ...

  10. DotNetCore跨平台~文章索引~永久更新

    本索引目录主要包括仓储大叔对dotnet core架构的研究与知识积累,从2016年开始进行撰写,到今天已经有一年多了,其中有一些小知识,小技巧,小应用,希望给大家在开发时一些启发,也希望dotnet ...