Skip to content

Commit

Permalink
Create Remove every kth node
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Apr 29, 2024
1 parent 4a5d00b commit 10b808f
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions Remove every kth node
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//Remove every kth node

import java.util.*;

class Node {
Node next;
int data;

Node(int d) {
data = d;
next = null;
}
}

class Delete_Kth_Node {
Node head;
Node tail;

void addToTheLast(Node node) {
if(head == null) {
head = node;
tail = node;
}

else {
tail.next = node;
tail = tail.next ;
}
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();

while(t>0) {
int n = sc.nextInt();
Delete_Kth_Node list = new Delete_Kth_Node();
int a1=sc.nextInt();
Node head= new Node(a1);
list.addToTheLast(head);

for (int i = 1; i < n; i++) {
int a = sc.nextInt();
list.addToTheLast(new Node(a));
}

int k = sc.nextInt();
list.head = new Solution().delete(list.head,k);
Node temp = list.head;

while(temp!=null) {
System.out.print(temp.data+ " ");
temp = temp.next;
}
System.out.println();
t--;
}
}
}

class Node {
Node next;
int data;

Node(int d) {
data = d;
next = null;
}
}

class Solution {
Node delete(Node head, int k) {
if(k == 1) {
return null;
}

int n = 1;
Node ptr = head;
Node delp = null;

while(ptr != null) {
if(n == k) {
n = 0;
delp.next = ptr.next;
}

else {
delp = ptr;
}

n++;
ptr = ptr.next;
}

return head;
}
}

0 comments on commit 10b808f

Please sign in to comment.