Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- int t = sc.nextInt();
- for (int j = 0; j < t; j++)
- {
- int n = sc.nextInt();
- int k = sc.nextInt();
- LL list = new LL();
- for (int i = 0; i < n; i++)
- {
- list.add(sc.nextInt());
- }
- Node temp = list.removeKth(list.head, k, n);
- list.printlist(temp);
- System.out.println();
- }
- }
- }
- class Node
- {
- int data;
- Node next;
- Node(int data)
- {
- this.data = data;
- this.next = next;
- }
- }
- class LL
- {
- Node head;
- Node tail;
- public void add(int data)
- {
- Node newNode = new Node(data);
- if (head == null)
- {
- head = newNode;
- tail=newNode;
- return;
- }
- tail.next= newNode;
- tail=tail.next;
- }
- public Node removeKth(Node head, int k, int n)
- {
- Node temp = head;
- if (head.next == null)
- {
- return null;
- }
- if (n == k )
- {
- return temp.next;
- }
- for (int i = 1; i < n - k; i++)
- {
- temp = temp.next;
- }
- temp.next = temp.next.next;
- return head;
- }
- public void printlist(Node head)
- {
- if (head == null)
- {
- return;
- }
- Node temp = head;
- while (temp != null)
- {
- System.out.print(temp.data + " ");
- temp = temp.next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement