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 ...
随机推荐
- Builder模式详解及其在Android开发中的应用
一.引言 在Android开发中,采用Builder模式的代码随处可见,比如说Android系统对话框AlertDialog的使用或者是Android中的通知栏(Notification)的使用,又比 ...
- Linux网络编程“惊群”问题总结
1.前言 我从事Linux系统下网络开发将近4年了,经常还是遇到一些问题,只是知其然而不知其所以然,有时候和其他人交流,搞得非常尴尬.如今计算机都是多核了,网络编程框架也逐步丰富多了,我所知道的有多进 ...
- php变量双击选择无法选择$符号
创建/Data/Packages/User/PHP.sublime-settings文件,内容为 { "word_separators": "./\\()\&qu ...
- svn文件图
- jq和js插件的各个文件夹里放置的内容
1. demo文件夹,存放各种实例. 2. dist文件夹,全称是distribution.在某些框架中,因为开发和发布的内容或者代码形式是不一样的(比如利用Grunt压缩等等),这时候就需要一个存放 ...
- 【原创】源码角度分析Android的消息机制系列(六)——Handler的工作原理
ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 先看Handler的定义: /** * A Handler allows you to send and process {@link Mes ...
- 信息安全的核心:CIA三元组 | 安全千字文系列1
我们总是在说信息安全管理,那么信息安全管理到底是在管什么?我们要如何定义信息安全? 这里就要引出信息安全最基本的概念:CIA三元组. 这里的 C,指的是Confidentiality机密性 这里的 I ...
- powerdesinger(MSSQLSRV2008测试通过)通过Name或comment 导出注释到sql脚本,生成sql的说明备注,包括表注释信息
导出字段信息name注释到sql2008字段的说明 在database -> edit current dbms -> MSSQLSRV2008::Script\Objects\Colum ...
- awvs 中文手册详细版
目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...
- peoplesoft function PSTREENODE 通过 deptid 获得部门树 全路径 名称
create or replace function getUnitFullName(deptid in varchar) return varchar2 is r ); c int; n ); m ...