in this second for loop used ,so Is it for traverse the array or queue?
First negative integer
@tanishka972,
In the first for loop the first k elements or we can say the first window is processed.
In the second for loop the remaining elements are processed i.e. from arr[k] to arr[n-1]
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
public void appendLastN(int n) throws Exception {
LinkedList nl=new LinkedList();
n=n%this.size;
if(this.size==n)
return;
while(this.size!=n)
{
int temp=removeFirst();
nl.addLast(temp);
}
for(int i=0; i < n; i++)
{
int temp=removeLast();
nl.addFirst(temp);
}
this.head=nl.head;
this.tail=nl.tail;
this.tail.next=null;
this.size=nl.size;
}
In this code the argument we pass as int n.for what we use it here(appendLastN(int n) )?
i dont understand this
n=n%this.size;
@tanishka972,
We do that because if size of list is 7 and now value of n can be 3,10,17 it doesn’t matter, we will be appending 3 elements only. So that’s why we do n=n%this.size; to avoid extra iterations
I want to ask that this n is what? this value is for those 7 elements or no. of elements or the elements from where we put list at start.
for eg if we have 5 elements
1 2 3 4 5 and we put two elements at start the it coul be like
2=2/5 i.e, = 0.1 so what it mean?
for eg if we have 5 elements
1 2 3 4 5 and we put two elements at start the it coul be like
2=2/5 i.e, = 0.1 so what it mean?
if number of elements are 5. Say 1 2 3 4 5 and we need to put 2 elements at the start, that is n = 2.
we will do:
n = 2%5 = 2 (it is modulo not divide)
Our final result will be 4 5 1 2 3
Now lets say that n = 7.
Here also our final linked list will be 4 5 1 2 3. ( at n = 5, the linked list will become the same)
Hence we do n = n % this.size because when n > size, we need only need to move n%this.size elements at the start
ok ! Thank you for solving my doubt 