Question : Reverse a Linked List in groups of given size K.
问题:以K个元素为一组逆转链表。
/**
* @author YuHuang
* @vision 2011-10-03
* This program is only for algorithm training.
*
*/
class Node {
public int data;
public Node next;
}
class LinkedList {
private Node head;
private int size;
public LinkedList(int size){
this(new int[size]);
}
public LinkedList(int[] initArray){
int len = initArray.length;
if(len>0){
int count=len;
Node preNode=null;
while(--count>=0){
Node node = new Node();
node.data=initArray[count];
node.next=preNode;
preNode=node;
}
head = new Node();
head.next=preNode;
}
this.size = len;
}
public Node getHead() {
return this.head;
}
public void resetHead(Node node){
this.head.next=node;
}
}
public class ReverseLinkedListByGroup {
public static void doReverse(LinkedList list,int k){
Node prev=null,next=null;
Node r=null;
Node head=null;
int count;
if(list==null){
return;
}
Node current = list.getHead().next;
Node p;
while(current!=null){
count=k;
p=current;
while(current!=null && --count>=0){
next = current.next;
current.next=prev;
prev = current;
current = next;
}
if(r==null) {
r=prev;
}else{
head.next=prev;
}
head=p;
head.next=null; //remember to set null to head.next in order to stop the progrom
}
list.resetHead(r);
}
public static void main(String[] args){
int[] initArray=new int[]{1,2,3,4,5,6,7,8,9,10};
LinkedList list=new LinkedList(initArray);
Node node = list.getHead().next;
System.out.println("Before Reversed : ");
while(node!=null){
System.out.print(node.data+" ");
node = node.next;
}
System.out.println();
ReverseLinkedListByGroup.doReverse(list,3);
node=list.getHead().next;
System.out.println("After Reversed : ");
while(node!=null){
System.out.print(node.data+" ");
node = node.next;
}
System.out.println();
}
}
运行结果为:
Lab-Computer-0db2f6:JavaExercises labuser$ java ReverseLinkedListByGroup
Before Reversed :
1 2 3 4 5 6 7 8 9 10
After Reversed :
3 2 1 6 5 4 9 8 7 10
分享到:
相关推荐
赠送jar包:pentaho-aggdesigner-algorithm-5.1.5-jhyde.jar; 赠送原API文档:pentaho-aggdesigner-algorithm-5.1.5-jhyde-javadoc.jar; 赠送源代码:pentaho-aggdesigner-algorithm-5.1.5-jhyde-sources.jar; ...
java.lang.RuntimeException: Unsupported algorithm: HmacSHA1 解决方法,阿里云
Automated Algorithm Selection: Survey and Perspectives。今年的新survey,对算法自动调参领域作了详尽介绍。
论文研究-SGA(Simplex-Genetic Algorithm):一类求解Minimax问题的通用算法.pdf, 在指出一般的迭代法不能保证收敛性之后,将注意力投向基于Stackelberg-NashEquilibrium的...
产生可能的集合 68 30.Algorithm Gossip: m元素集合的n个元素子集 71 31.Algorithm Gossip: 数字拆解 73 32.Algorithm Gossip: 得分排行 76 33.Algorithm Gossip: 选择、插入、气泡排序 78 34....
5.Algorithm Gossip: 老鼠走迷官(一) 6.Algorithm Gossip: 老鼠走迷官(二) 7.Algorithm Gossip: 骑士走棋盘 8.Algorithm Gossip: 八皇后 9.Algorithm Gossip: 八枚银币. 10.Algorithm Gossip: 生命游戏. 11....
巴斯卡三角形4.Algorithm Gossip: 三色棋5.Algorithm Gossip: 老鼠走迷官(一6.Algorithm Gossip: 老鼠走迷官(二7.Algorithm Gossip: 骑士走棋盘8.Algorithm Gossip: 八皇后9.Algorithm Gossip: 八枚银币10....
DGA- An Efficient Genetic Algorithm DGA:一种高效的遗传算法.doc
可以解决,maven引入hive jar包时,hive Could not find artifact org.pentaho:pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde 问题
解决 Cannot resolve org.pentaho:pentaho-aggdesigner-algorithm:5.1.5-jhyde jar放入D根目录执行: 以下命令加入本地maven库 mvn install:install-file -DgroupId=org.pentaho -DartifactId=pentaho-aggdesigner-...
2.Algorithm Gossip: 费式数列 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官 6.Algorithm Gossip: 老鼠走迷官(二) 7.Algorithm Gossip: 骑士走棋盘 8.Algorithm Gossip: 八皇 9....
解决maven引入hive的jar包时依赖报错Could not find artifact org.pentaho:pentaho-aggdesigner-algorithm:pom:5.1.5-jhyde in xxx的问题,maven路径org/pentaho/pentaho-aggdesigner-algorithm/5.1.5-jhyde/pentaho...
所提出的算法基于 k 最近邻方法,其中 k 的值是唯一的算法参数,用于控制最终解决方案的“平滑度”。 该算法的思想属于: 莫雷拉、阿德里亚诺... Concave hull:用于计算由一组点占据的区域的 k 最近邻方法.. 61-68。
algorithm design foundations analysis and internet examples 中文版
* 问题:* 1. 索引单向链表中的元素* 2. 边界问题* 思路:* 1. 顺序遍历* 2. trick,两个前后间隔相差 k 的指针顺序遍历整个单向链表到末
面向初学者,深入浅出,通俗易懂的算法介绍。Once Upon an Algorithm: How Stories Explain Computing
老鼠走迷宫(二)7.Algorithm Gossip: 骑士走棋盘8.Algorithm Gossip: 八皇后9.Algorithm Gossip: 八枚银币10.Algorithm Gossip: 生命游戏11.Algorithm Gossip: 字串核对12.Algorithm Gossip: 双色、三色河内塔13....