Advertisement
unknown_0711

Untitled

Oct 30th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main{
  6.  
  7. public static Scanner sc=new Scanner(System.in);
  8.  
  9. public static class Node
  10. {
  11. int data;
  12. Node next;
  13. Node(int value)
  14. {
  15. this.data=value;
  16. this.next=null;
  17. }
  18. }
  19.  
  20. public static void fun()
  21. {
  22. int n=sc.nextInt();
  23. int pos=sc.nextInt();
  24. Node head=null;
  25. Node last=null;
  26.  
  27. for(int i=0;i<n;i++)
  28. {
  29. int v=sc.nextInt();
  30. if(head==null)
  31. {
  32. head=new Node(v);
  33. last=head;
  34. } else
  35. {
  36. last.next=new Node(v);
  37. last=last.next;
  38. }
  39. }
  40.  
  41. int newpos=n-pos+1;
  42.  
  43. if(newpos==1)
  44. {
  45. Node prev=head;
  46. head=head.next;
  47. prev=null;
  48. } else
  49. {
  50. Node prev=head;
  51. for(int i=2;i<newpos;i++)
  52. {
  53. prev=prev.next;
  54. }
  55. Node target=prev.next;
  56. prev.next=target.next;
  57. target=null;
  58. }
  59.  
  60. Node n1=head;
  61. while(n1!=null)
  62. {
  63. System.out.print(n1.data+" ");
  64. n1=n1.next;
  65. }
  66. return ;
  67. }
  68.  
  69. public static void main(String[] args)
  70. {
  71. int tc=sc.nextInt();
  72.  
  73. for(int i=0;i<tc;i++)
  74. {
  75. fun();
  76. System.out.println();
  77. }
  78.  
  79. return ;
  80. }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement