Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ReverseKElements {
- public static SLLNode<Integer> getEl(SLL<Integer> list, int k){
- SLLNode<Integer> tmp=list.getFirst();
- k--;
- while (k!=0){
- k--;
- if(tmp.succ==null){
- return tmp;
- }
- tmp=tmp.succ;
- }
- return tmp;
- }
- public static void reverse(SLL<Integer> list, int k){
- SLLNode<Integer> tmp=list.getFirst();
- int copyK;
- int orgK=k;
- while (tmp!=null){
- copyK=orgK-1;
- while (copyK!=0){
- copyK--;
- SLLNode<Integer> node=getEl(list,k);
- list.insertBefore(node.element,tmp);
- list.delete(node);
- if(tmp.succ==null) break;
- }
- k*=2;
- if(k>list.length()){
- k=list.length();
- }
- tmp=tmp.succ;
- }
- System.out.println(list);
- }
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int n=in.nextInt();
- SLL<Integer> list=new SLL<>();
- for(int i=0;i<n;i++){
- list.insertLast(in.nextInt());
- }
- int k=in.nextInt();
- reverse(list,k);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement