In: Computer Science
Consider a linked list whose nodes are objects of the class Node:
class Node {
public int data;
public Node next;
}
prev references a node n1 in the
list and curr references the node
n2 that is right after n1 in the
list. Which of the following statements is used to insert a new
node, referenced by newNodebetween
prev and curr?
Group of answer choices
newNode.next = curr;
prev.next = newNode;
newNode.next = head;
head = newNode;
prev.next = newNode;
newNode.next = prev;
prev.next = curr;
newNode.next = curr;
none of the above
Which of the following statements deletes the first node of a linked list implementation of ListInterface that has 10 nodes? Assume curr refers to the second node in the list and the head of the list is the reference variable fistNode
Group of answer choices
A. firstNode.next = curr.next;
B. firstNode = curr.next;
C. firstNode = firstNode.next;
D. curr = firstNode.next;
E. B and C
In LList2 implementation of the ListInterface,
Group of answer choices
A. The worst-case runtime of contains() is independent of the number of items in LinkedList
B. The runtime of getLength() is independent of the number of items in LinkedList
C. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList
D. A and B
E. B and C
In a linked implementation of ListInterface, if the reference to the first node is null, this means
Group of answer choices
the list is full
the garbage collector should be invoked
we can't add items to the list
the list is in an unstable state
the list is empty
Answer 1 :
If prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list.
Then , statements required to insert a new node, referenced by newNode between prev and curr are :
newNode.next = curr;
prev.next = newNode;
Answer 2 : Correct Option is Option(C) ie. firstNode = firstNode.next;
If curr refers to the second node in the list and the head of the list is the reference variable fistNode.
So, the statement that deletes the first node of a linked list implementation of ListInterface that has 10 nodes are :
firstNode = firstNode.next;
Answer 3 : Correct Option is Option(C) ie. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList.
Explanation:
Option(A) is Incorrect. As in he worst-case runtime of contains() is dependent of the number of items in LinkedList. And is directly proportional to the number of items in the Linked List.
Option(B) is Incorrect. The runtime of getLength() is dependent of the number of items in LinkedList. As it is depends upon the total numbers of items in the LinkedList.
Option(C) is Correct. The runtime of remove(1) (removing the first element) is independent of the number of items in LinkedList. It will take constant time to remove first element irrespective of the size of LinkedList.
Option(D) hence are Incorrect.
Option(E) hence are Incorrect,
Answer 4 : Correct Answer is Option(E) ie. The list is Empty.
Explanation :
Option(A) is Incorrect. the list is not full.
Option(B) is Incorrect. It doesnot mean the garbage collector should be invoked
Option(C) is Incorrect. It doesnot mean that we cannot add items to the list.
Optioon(D) is Incorrect. It doesn't mean the list is in an unstable state
Option(E) is Correct. That it means the list is empty.
Hence,
Option(E) is the correct Option ie. the list is empty.