Question

Which of the following code snippets correctly implements a singly linked list in Java, including the ability to insert a new node at the beginning? class Node {     int data;     Node next;         Node( int data) {         this .data = data;         this .next = null ;     }}   class LinkedList {     Node head;         LinkedList() {         head = null ;     }          void insertAtBeginning ( int data) {         Node newNode = new Node (data);         newNode.next = head;         head = newNode;     }}

A The code correctly implements a singly linked list with the insertion at the beginning.
B The insertAtBeginning method should not be in the LinkedList class
C The Node constructor does not correctly initialize the next reference
D The insertAtBeginning method incorrectly modifies the head of the list.
E The code should implement a circular linked list, not a singly linked list.
Practice Next

Hey! Ask a query