Advertisement
unknown_0711

Untitled

Oct 21st, 2022
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 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 void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner sc = new Scanner(System.in);
  10. int t = sc.nextInt();
  11. for (int j = 0; j < t; j++)
  12. {
  13. int n = sc.nextInt();
  14. int k = sc.nextInt();
  15. LL list = new LL();
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. list.add(sc.nextInt());
  20. }
  21. Node temp = list.removeKth(list.head, k, n);
  22. list.printlist(temp);
  23. System.out.println();
  24. }
  25. }
  26. }
  27. class Node
  28. {
  29. int data;
  30. Node next;
  31. Node(int data)
  32. {
  33. this.data = data;
  34. this.next = next;
  35. }
  36. }
  37. class LL
  38. {
  39. Node head;
  40. Node tail;
  41. public void add(int data)
  42. {
  43. Node newNode = new Node(data);
  44. if (head == null)
  45. {
  46. head = newNode;
  47. tail=newNode;
  48. return;
  49. }
  50. tail.next= newNode;
  51. tail=tail.next;
  52. }
  53. public Node removeKth(Node head, int k, int n)
  54. {
  55. Node temp = head;
  56. if (head.next == null)
  57. {
  58. return null;
  59. }
  60. if (n == k )
  61. {
  62. return temp.next;
  63. }
  64. for (int i = 1; i < n - k; i++)
  65. {
  66. temp = temp.next;
  67. }
  68. temp.next = temp.next.next;
  69. return head;
  70. }
  71. public void printlist(Node head)
  72. {
  73. if (head == null)
  74. {
  75. return;
  76. }
  77. Node temp = head;
  78. while (temp != null)
  79. {
  80. System.out.print(temp.data + " ");
  81. temp = temp.next;
  82. }
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement