Nikhil
BAN USERimport java.util.Stack;
public class SkipReverseKNodes {
public void reverse(Node head,int k){
if(k<=1 || k>=head.getSize()){
System.out.println("Enter a value greater than 1 and less than "+head.getSize());
return;
}
Node runner=head;
Node current=head;
for(int i=0;i<k-1;i++){
runner=runner.next;
current=current.next;
}
runner=current.next;
while(runner!=null){
Stack<Integer> s=new Stack<Integer>();
for(int i=0;i<k;i++){
s.push(runner.data);
runner=runner.next;
if(runner==null) break;
}
while(!s.isEmpty()){
current.next.data=s.pop();
current=current.next;
}
}
}
public static void main(String[] args) {
Node head=new Node(1);
head.appendNode(2);
head.appendNode(3);
head.appendNode(4);
head.appendNode(5);
head.appendNode(6);
head.appendNode(7);
head.appendNode(8);
SkipReverseKNodes s=new SkipReverseKNodes();
Node n=head;
while(n!=null){
System.out.print(n.data+" ");
n=n.next;
}
System.out.println();
s.reverse(head, 2);
n=head;
while(n!=null){
System.out.print(n.data+" ");
n=n.next;
}
}
}
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
int in[]={1,2,2,2,2,2,3,4,5,6,7,8,9,10};
int key=2;
Test test=new Test();
ArrayList<Integer> indexList=test.getIndexes(in,key);
System.out.println(indexList);
}
private ArrayList<Integer> getIndexes(int[] in, int key) {
ArrayList<Integer> indexList=new ArrayList<Integer>();
for(int i=0;i<in.length;i++){
if(in[i]==key)
indexList.add(i);
}
return indexList;
}
}
- Nikhil June 11, 2015