adplus-dvertising
frame-decoration

Question

Which of the following piece of code removes the node from a given position?
a)

public void remove(int pos)
{
	if(pos<0 || pos>=size)
	{
		System.out.println("Invalid position");
		return;
	}
	else
	{
		if(head == null)
			return;
		if(pos == 0)
		{
			head = head.getNext();
			if(head == null)
			tail = null;
		}
	        else
	        {
			Node temp = head;
			for(int i=1; i<position; i++)
			temp = temp.getNext();
		}
		temp.getNext().setPrev(temp.getPrev());
		temp.getPrev().setNext(temp.getNext());
	}
	size--;
}

b)

public void remove(int pos)
{
	if(pos<0 || pos>=size)
	{
		System.out.println("Invalid position");
		return;
	}
	else
	{
		if(head == null)
		return;
		if(pos == 0)
		{
			head = head.getNext();
			if(head == null)
			tail = null;
		}
		else
		{
			Node temp = head;
			for(int i=1; i<position; i++)
			temp = temp.getNext();
		}
		temp.getNext().setPrev(temp.getNext());
		temp.getPrev().setNext(temp.getPrev());
	}
	size--;
}

c)

public void remove(int pos)
{
	if(pos<0 || pos>=size)
	{
		System.out.println("Invalid position");
		return;
	}
	else
	{
		if(head == null)
			return;
		if(pos == 0)
		{
			head = head.getNext();
			if(head == null)
			tail = null;
		}
		else
		{
			Node temp = head;
			for(int i=1; i<position; i++)
			temp = temp.getNext().getNext();
		}
		temp.getNext().setPrev(temp.getPrev());
		temp.getPrev().setNext(temp.getNext());
	}
	size--;
}

d) None of the mentioned

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. Which of the following piece of code removes the node from a given position?

Similar Questions

Discover Related MCQs

Q. How do you calculate the pointer difference in a memory efficient double linked list?

Q. What is the time complexity of inserting a node in a doubly linked list?

Q. Consider the 2-level skip list. How to access 38?

Q. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head pointer only.
Given the representation, which of the following operation can be implemented in O(1) time?

i) Insertion at the front of the linked list

ii) Insertion at the end of the linked list

iii) Deletion of the front node of the linked list

iv) Deletion of the last node of the linked list

Q. In linked list each node contain minimum of two fields. One field is data field to store the data second field is?

Q. What would be the asymptotic time complexity to add an element in the linked list?

Q. What would be the asymptotic time complexity to find an element in the linked list?

Q. What would be the asymptotic time complexity to insert an element at the second position in the linked list?

Q. The concatenation of two list can performed in O(1) time. Which of the following variation of linked list can be used?

Q. Linked lists are not suitable to for the implementation of?

Q. Linked list is considered as an example of ___________ type of memory allocation.

Q. In Linked List implementation, a node carries information regarding

Q. Linked list data structure offers considerable saving in

Q. Which of the following is not a disadvantage to the usage of array?

Q. What is the time complexity of inserting at the end in dynamic arrays?

Q. What is the time complexity to count the number of elements in the linked list?

Q. What is the space complexity for deleting a linked list?

Q. Which of these is an application of linked lists?

Q. What differentiates a circular linked list from a normal linked list?

Q. What is the time complexity of searching for an element in a circular linked list?