COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Question
[CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
|
How do you insert an element at the beginning of the list?
|
public void insertBegin(Node node){ node.setNext(head); head = node; size++;}
|
|
public void insertBegin(Node node){ head = node; node.setNext(head); size++;}
|
|
public void insertBegin(Node node){ head = node; node.setNext(head); size++;}
|
|
public void insertBegin(Node node){ Node temp = head.getNext() node.setNext(temp); head = node; size++;}
|
|
public void insertBegin(Node node){ Node temp = head.getNext() node.setNext(temp); node = head; size++;}
|
Explanation:
Detailed explanation-1: -How do you insert an element at the beginning of the list? Explanation: Set the ‘next’ pointer point to the head of the list and then make this new node as the head.
Detailed explanation-2: -Inserting at the Beginning This involves pointing the next pointer of the new data node to the current head of the linked list. So the current head of the linked list becomes the second data element and the new node becomes the head of the linked list.
Detailed explanation-3: -Elements can be added at the beginning of a LinkedList by using the method java. util. LinkedList. addFirst().
There is 1 question to complete.