Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class Node{
- int data;
- Node next;
- public Node(int data){
- this.data=data;
- }
- }
- public class Main
- {
- static Node head;
- static Node tail;
- public static int countNode(){
- Node curr=head;
- int count =0;
- while(curr!=null){
- curr=curr.next;
- count++;
- }
- return count;
- }
- public static void display(){
- Node curr= head;
- while(curr!=null){
- System.out.print(curr.data+" ");
- curr=curr.next;
- }
- System.out.println();
- }
- public static void addLast(int data){
- Node nn = new Node(data);
- if(head==null){
- head= tail =nn;
- }else{
- tail.next=nn;
- tail=nn;
- }
- }
- public static Node removemid(){
- Node curr=head;
- if (curr == null) // for 0 element
- return null;
- if (curr.next == null) { // for 1 eelmwnt
- return null;
- }
- int count=countNode(); //front count fun.
- int mid =count/2; // stop before mid.
- for(int i=0;i<mid-1;i++)
- curr=curr.next;
- curr.next=curr.next.next;
- return curr;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc= new Scanner(System.in);
- int k=sc.nextInt();
- for(int j=0;j<k;j++){
- int n=sc.nextInt();
- head=null;
- tail=null;
- for(int i=1;i<=n;i++){
- int a=sc.nextInt();
- addLast(a);
- }
- removemid();
- display();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement