Advertisement
unknown_0711

Untitled

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