‹ Back to the paper
A linked list is formed from the objects of the class: class Node { int num; Node next; } Write an…
A linked list is formed from the objects of the class:
class Node
{
int num;
Node next;
}Write an Algorithm OR a Method to insert a node at the beginning of an existing linked list.
The method declaration is as follows:void InsertNode( Nodes starPtr, int n )Answer
Answer
AIAssuming a class-level Node reference `start` holds the head of the linked list, and `startPtr` is the current head passed into the method:
void InsertNode(Node startPtr, int n)
{
Node temp = new Node();
temp.num = n;
temp.next = startPtr;
start = temp;
}A new node `temp` is created and its data field is set to n; its `next` is made to point to the current first node (startPtr), and the class-level head pointer `start` is then updated to this new node, thereby inserting it at the beginning of the list.From ISC 2023 Specimen Computer Science Paper 1, question 11(i).