adplus-dvertising
frame-decoration

Question

Choose the code snippet which inserts a node to the head of the list?
a)

public void insertHead(int data)
{
 Node temp = new Node(data);
 Node cur = head;
 while(cur.getNext() != head)
  cur = cur.getNext()
 if(head == null)
 {
  head = temp;
  head.setNext(head);
 }
 else
 {
  temp.setNext(head);
  head = temp;
  cur.setNext(temp);
 }
 size++;
}

b)

public void insertHead(int data)
{
 Node temp = new Node(data);
 while(cur != head)
  cur = cur.getNext()
 if(head == null)
 {
  head = temp;
  head.setNext(head);
 }
 else
 {
  temp.setNext(head.getNext());
  cur.setNext(temp);
 }
 size++;
}

c)

public void insertHead(int data)
{
 Node temp = new Node(data);
 if(head == null)
 {
  head = temp;
  head.setNext(head);
 }
 else
 {
  temp.setNext(head.getNext());
  head = temp;
 }
 size++;
}

d)

public void insertHead(int data)
{
 Node temp = new Node(data);
 if(head == null)
 {
  head = temp;
  head.setNext(head.getNext());
 }
 else
 {
  temp.setNext(head.getNext());
  head = temp;
 }
 size++;
}

a.

a

b.

b

c.

c

d.

d

Answer: (a).a

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Choose the code snippet which inserts a node to the head of the list?

Similar Questions

Discover Related MCQs

Q. Which of the following is false about a circular linked list?

Q. Consider a small circular linked list. How to detect the presence of cycles in this list effectively?

Q. What is a skip list?

Q. Skip lists are similar to which of the following datastructure?

Q. What is the time complexity improvement of skip lists from linked lists in insertion and deletion?

Q. To which datastructure are skip lists similar to in terms of time complexities in worst and best cases?

Q. The nodes in a skip list may have many forward references. their number is determined

Q. Are the below statements true about skiplists?
In a sorted set of elements skip lists can implement the below operations

i.given a element find closest element to the given value in the sorted set in O(logn)

ii.find the number of elements in the set whose values fall a given range in O(logn)

Q. How to maintain multi-level skip list properties when insertions and deletions are done?

Q. Is a skip list like balanced tree?

Q. What is indexed skip list?

Q. What is xor linked list ?

Q. What does a xor linked list have ?

Q. What does first and last nodes of a xor linked lists contain ? (let address of first and last be A and B)

Q. Disadvantages of xor lists

Q. What are the important properties of xor lists

Q. Which of the following statements are true ?
i) practical application of XOR linked lists are in environments with limited space requirements, such as embedded devices.

ii)xor lists are not suitable because most garbage collectors will fail to work properly with classes or structures that don’t contain literal pointers

iii)in order to calculate the address of the next node you need to remember the address of the previous node

iv)xor lists are much efficient than single, doubly linked lists and arrays

Q. Given 10,8,6,7,9
swap the above numbers such that finally you got 6,7,8,9,10

so now reverse 10

9,7,6,8,10

now reverse 9

8,6,7,9,10

7,6,8,9,10

6,7,8,9,10

At this point 6 is ahead so no more reversing can be done so stop.

To implement above algorithm which datastructure is better and why ?

Q. Free lists are used in

Q. What are implicit and explicit implementations of freelists?