Data Structure Questions and Answers – Singly Linked List

«
»

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Singly Linked List”.

1. Which of the following is not a disadvantage to the usage of array?
a) Fixed size
b) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
c) Insertion based on position
d) Accessing elements at specified positions
View Answer

Answer: d
Explanation: Array elements can be accessed in two steps. First, multiply the size of the data type with the specified position, second, add this value to the base address. Both of these operations can be done in constant time, hence accessing elements at a given index/position is faster.
advertisement

2. What is the time complexity of inserting at the end in dynamic arrays?
a) O(1)
b) O(n)
c) O(logn)
d) Either O(1) or O(n)
View Answer

Answer: d
Explanation: Depending on whether the array is full or not, the complexity in dynamic array varies. If you try to insert into an array that is not full, then the element is simply stored at the end, this takes O(1) time. If you try to insert into an array which is full, first you will have to allocate an array with double the size of the current array and then copy all the elements into it and finally insert the new element, this takes O(n) time.

3. What is the time complexity to count the number of elements in the linked list?
a) O(1)
b) O(n)
c) O(logn)
d) O(n2)
View Answer

Answer: b
Explanation: To count the number of elements, you have to traverse through the entire list, hence complexity is O(n).
advertisement
advertisement

4. Which of the following performs deletion of the last element in the list? Given below is the Node class.

class Node
{
	protected Node next;
	protected Object ele;
	Node(Object e,Node n)
	{
		ele = e;
		next = n;
	}
	public void setNext(Node n)
	{
		next = n;
	}
	public void setEle(Object e)
	{
		ele = e;
	}
	public Node getNext()
	{
		return next;
	}
	public Object getEle()
	{
		return ele;
	}
}
class SLL 
{
	Node head;
	int size;
	SLL()
	{
		size = 0;
	}
}

a)

advertisement
public Node removeLast()
{
	if(size == 0)
	return null;
	Node cur;
	Node temp;
	cur = head;
	while(cur.getNext() != null)
	{
		 temp = cur;
		 cur = cur.getNext();
        }
	temp.setNext(null);
	size--;
	return cur;
}

b)

advertisement
public void removeLast()
{
	if(size == 0)
	return null;
	Node cur;
	Node temp;
	cur = head;
	while(cur != null)
	{
		temp = cur;
		cur = cur.getNext();
        }
	temp.setNext(null);
	return cur;
}

c)

advertisement
public void removeLast()
{
	if(size == 0)
	    return null;
	Node cur;
	Node temp;
	cur = head;
	while(cur != null)
	{
		cur = cur.getNext();
		temp = cur;
	 }
	temp.setNext(null);
	return cur;
}

d)

public void removeLast()
{
	if(size == 0)
		return null;
	Node cur;
	Node temp;
	cur = head;
	while(cur.getNext() != null)
	{
		cur = cur.getNext();
		temp = cur;
	}
	temp.setNext(null);
	return cur;
}
View Answer
Answer: a
Explanation: Since you have to traverse to the end of the list and delete the last node, you need two reference pointers. ‘cur’ to traverse all the way and find the last node, and ‘temp’ is a trailing pointer to ‘cur’. Once you reach the end of the list, setNext of ‘temp’ to null, ‘cur’ is not being pointed to by any node, and hence it is available for garbage collection.
 
 

5. What is the functionality of the following code?

public void function(Node node)
{
	if(size == 0)
		head = node;
	else
	{
		Node temp,cur;
		for(cur = head; (temp = cur.getNext())!=null; cur = temp);
		cur.setNext(node);
	}
	size++;
}

a) Inserting a node at the beginning of the list
b) Deleting a node at the beginning of the list
c) Inserting a node at the end of the list
d) Deleting a node at the end of the list
View Answer

Answer: c
Explanation: The for loop traverses through the list and then inserts a new node as cur.setNext(node);

6. What is the space complexity for deleting a linked list?
a) O(1)
b) O(n)
c) Either O(1) or O(n)
d) O(logn)
View Answer

Answer: a
Explanation: You need a temp variable to keep track of current node, hence the space complexity is O(1).

7. How would you delete a node in the singly linked list? The position to be deleted is given.
a)

public void delete(int pos)
{
	if(pos < 0)
	pos = 0;
	if(pos > size)
	pos = size;
	if( size == 0)
	return;
	if(pos == 0)
	head = head.getNext();
	else
	{
	    Node temp = head;
	    for(int i=1; i<pos; i++)
            {
		temp = temp.getNext();
            }
	    temp.setNext(temp.getNext().getNext());
	}
	    size--;
}

b)

public void delete(int pos)
{
	if(pos < 0)
	pos = 0;
	if(pos > size)
	pos = size;
	if( size == 0)
	return;
	if(pos == 0)
	head = head.getNext();
	else
	{
	    Node temp = head;
	    for(int i=1; i<pos; i++)
	    {
		temp = temp.getNext();
	    }
	    temp.setNext(temp.getNext());
	}
	    size--;
}

c)

public void delete(int pos)
{
        if(pos < 0)
	pos = 0;
	if(pos > size)
	pos = size;
	if( size == 0)
	return;
	if(pos == 0)
	head = head.getNext();
	else
	{
	    Node temp = head;
	    for(int i=1; i<pos; i++)
	    {
		temp = temp.getNext().getNext();
            }
	    temp.setNext(temp.getNext().getNext());
	}
	    size--;
}

d)

public void delete(int pos)
{
        if(pos < 0)
        pos = 0;
        if(pos > size)
        pos = size;
        if( size == 0)
	return;
	if(pos == 0)
	head = head.getNext();
	else
	{
	    Node temp = head;
	    for(int i=0; i<pos; i++)
	    {
		temp = temp.getNext();
	    }
	    temp.setNext(temp.getNext().getNext());
	}
	size--;
}
View Answer
Answer: a
Explanation: Loop through the list to get into position one behind the actual position given. temp.setNext(temp.getNext().getNext()) will delete the specified node.
 
 

8. Which of these is not an application of a linked list?
a) To implement file systems
b) For separate chaining in hash-tables
c) To implement non-binary trees
d) Random Access of elements
View Answer

Answer: d
Explanation: To implement file system, for separate chaining in hash-tables and to implement non-binary trees linked lists are used. Elements are accessed sequentially in linked list. Random access of elements is not an applications of linked list.

9. Which of the following piece of code has the functionality of counting the number of elements in the list?
a)

public int length(Node head)
{
	int size = 0;
	Node cur = head;
	while(cur!=null)
	{
	    size++;
	    cur = cur.getNext();
	}
	return size;
}

b)

public int length(Node head)
{
        int size = 0;
	Node cur = head;
	while(cur!=null)
	{
	    cur = cur.getNext();
	    size++;
	}
	return size;
}

c)

public int length(Node head)
{
	int size = 0;
	Node cur = head;
	while(cur!=null)
	{
	    size++;
	    cur = cur.getNext();
	}
}

d)

public int length(Node head)
{
	int size = 0;
	Node cur = head;
	while(cur!=null)
	{
	    size++;
	    cur = cur.getNext().getNext();
	}
	return size;
}
View Answer
Answer: a
Explanation: ‘cur’ pointer traverses through list and increments the size variable until the end of list is reached.
 
 

10. How do you insert an element at the beginning of the list?
a)

public void insertBegin(Node node)
{
	node.setNext(head);
	head = node;
	size++;
}

b)

public void insertBegin(Node node)
{
	head = node;
	node.setNext(head);
	size++;
}

c)

public void insertBegin(Node node)
{
	Node temp = head.getNext()
	node.setNext(temp);
	head = node;
	size++;
}

d)

public void insertBegin(Node node)
{
	Node temp = head.getNext()
	node.setNext(temp);
	node = head;
	size++;
}
View Answer
Answer: a
Explanation: Set the ‘next’ pointer point to the head of the list and then make this new node as the head.
 
 

11. What is the functionality of the following piece of code?

public int function(int data)
{
	Node temp = head;
	int var = 0;
	while(temp != null)
	{
		if(temp.getData() == data)
		{
			return var;
		}
		var = var+1;
		temp = temp.getNext();
	}
	return Integer.MIN_VALUE;
}

a) Find and delete a given element in the list
b) Find and return the given element in the list
c) Find and return the position of the given element in the list
d) Find and insert a new element in the list
View Answer

Answer: c
Explanation: When temp is equal to data, the position of data is returned.

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter