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 ...
随机推荐
- Zookeeper:分布式程序的基石
一.目录 1.zookeeper是什么? 2.安装.配置.启动.监控 3.javaApi基础用法 4.应用场景 5.CAP理论/paxos算法 二.zookeeper简介 官方版:zookeeper是 ...
- oracle 树形表结构查询 排序
oracle 树形表结构排序 select * from Table start with parentid is null connect by prior id=parentid order SI ...
- jquery $.each 和for 怎么跳出循环
jquery $.each 和for 怎么跳出循环 1.for循环中我们使用continue:终止本次循环计入下一个循环,使用break终止整个循环.2.而在jquery中 $.each则对应的使用r ...
- Example017简单的下拉框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 8.vue的生命周期
Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁的过程,就是生命周期 ...
- JavaScript学习笔记(一)——初识js
这个周,开始了JavaScript的学习路程.虽然从高中开始就接触了网页设计的知识,大学里的学习也算回顾了Html和Css的知识,实习期间在牛盾科技也是做的网站建设,不过通过前段时间找工作才发现自己了 ...
- gulp 入门指南
gulp 是基于 node 实现 Web 前端自动化开发的工具,利用它能够极大的提高开发效率. 在 Web 前端开发工作中有很多"重复工作",比如压缩CSS/JS文件.而这些工作都 ...
- peoplesoft function PSTREENODE 通过 deptid 获得部门树 一级部门 code
create or replace function ht_gettopdeptid(deptid in varchar) return varchar2 is r ); c int; m ); r_ ...
- MyBatis源码解析【5】工厂的构建
前言 这个分类比较连续,如果这里看不懂,或者第一次看,请回顾之前的博客 http://www.cnblogs.com/linkstar/category/1027239.html 终于算是把装备弄齐全 ...
- 004.Create a web app with ASP.NET Core MVC using Visual Studio on Windows --【在 windows上用VS创建mvc web app】
Create a web app with ASP.NET Core MVC using Visual Studio on Windows 在 windows上用VS创建mvc web app 201 ...