My issue
Main.java:67: error: non-static variable this cannot be referenced from a static context
e = h = new Node(x);
^
Main.java:70: error: non-static variable this cannot be referenced from a static context
Node g = new Node(x);
getting this error…I am unable to fix as the codes are hidden
My code
class Node{
int val;
Node next;
Node(int x)
{
val = x;
next = null;
}
}
static Node rotateRight(Node head, int k){
int count = 0;
Node start = head;
//traverse till the end of the list and keep incrementing
//count
while (head.next != null) {
head = head.next;
count++;
}
count++;
// if k > count, do k%count, its an optimization. 2%5 == 12%5
k = k % count;
//find the new k
k = Math.abs(count - k);
if (k == 0)
return start;
//connect last element to start
head.next = start;
//traverse for new k value
while (k-- > 0) {
head = head.next;
}
// note: head is not the last element, so set the start.
start = head.next;
head.next = null;
return start;
}
Learning course: Placement preparation using Java
Problem Link: CodeChef: Practical coding for everyone