Only one testcase is not passing , please help
do n = n % length for cases where n > length
try to debug for this input:
4
1 2 3 4
4
correct output :
1 2 3 4
Sorry didn’t get you
i have added this line in my code
n = n% length
import java.util.*;
public class Main {
public static void appendLastN(int n) throws Exception {
Node temp = head;
n = n % size;
for(int i = 1 ; i < size - n ; i++) temp = temp.next;
Node p = temp.next;
temp.next = null;
tail.next = head;
head = p;
}
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int N = scn.nextInt();
for (int i = 0; i < N; i++) {
push(scn.nextInt());
}
int n= scn.nextInt();
appendLastN(n);
Node temp = head;
for(int i = 1;i<=size;i++){
System.out.print(temp.data+" ");
temp = temp.next;
}
}
public static class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null ;
}
}
public static Node head , tail;
public static int size = 0;
public static Node push(int data){
Node nn = new Node(data);
if(head == null) {
head = nn;
tail = nn;
size++;
return head;
}
Node temp;
for(temp = head ; temp.next != null ; temp = temp.next);
size++;
temp.next = nn;
tail = nn;
return head;
}
}