Here's the function to add node in singly link list. It is getting executed on Java 15, but for Java 8 it shows the compilation error.
public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
// Write your code here
SinglyLinkedListNode new_node = new SinglyLinkedListNode(data);
SinglyLinkedListNode curr_node = llist;
int i=0;
while(i<position-1)
{
curr_node = curr_node.next;
i++;
}
new_node.next = curr_node.next;
curr_node.next = new_node;
return llist;
}
Here's the link for whole problem statement: https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem
This is the error:
Solution.java:78: error: Illegal static declaration in inner class Solution.Result
public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
^
modifier 'static' is only allowed in constant variable declarations
Solution.java:121: error: cannot find symbol
SinglyLinkedListNode llist_head = insertNodeAtPosition(llist.head, data, position);
^
symbol: method insertNodeAtPosition(SinglyLinkedListNode,int,int)
location: class Solution
2 errors
Exit Status
1
What is the reason for this error?