Data Structure Linked list Interview Preparation Guide
Download PDF
Add New Question

Data Structure Linked list frequently Asked Questions in various Linked list job Interviews by interviewer. The set of Data Structure Linked list interview questions here ensures that you offer a perfect answer to the interview questions posed to you. Get preparation of Data Structure Linked list job interview

22 Linked list Questions and Answers:

1 :: Explain the steps to insert data into a singly linked list?

Steps to insert data into a singly linked list:-

Insert in middle
Data: 1, 2, 3, 5
1. Locate the node after which a new node (say 4) needs to be inserted.(say after 3)

2. create the new node i.e. 4

3. Point the new node 4 to 5 (new_node->next = node->next(5))

4. Point the node 3 to node 4 (node->next =newnode)

Insert in the beginning

Data: 2, 3, 5

1. Locate the first node in the list (2)

2. Point the new node (say 1) to the first node. (new_node->next=first) Insert in the end

Insert in the end

Data: 2, 3, 5

1. Locate the last node in the list (5)

2. Point the last node (5) to the new node (say 6). (node->next=new_node)
Post Your Answer

2 :: How to reverse singly link list?

Reverse a singly link list using iteration:-

1. First, set a pointer ( *current) to point to the first node i.e. current=base

2. Move ahead until current!=null (till the end)

3. set another pointer (* next) to point to the next node i.e. next=current->next

4. store reference of *next in a temporary variable ( *result) i.e current->next=result

5. swap the result value with current i.e. result=current

6. and now swap the current value with next. i.e. current=next

7. return result and repeat from step 2

A linked list can also be reversed using recursion which eliminates the use of a temporary variable.
Post Your Answer

3 :: What is circular linked list?

An array of pointers is an array consisting of pointers. Here, each pointer points to a row of the matrix or an element. E.g char *array [] = {“a”, “b”}. This is an array of pointers to to characters.
Post Your Answer

4 :: Explain circular linked list?

In a circular linked list the first and the last node are linked. This means that the last node points to the first node in the list. Circular linked list allow quick access to first and last record using a single pointer.
Post Your Answer

5 :: Explain linked list using C++ with an example?

A linked list is a chain of nodes. Each and every node has at least two nodes, one is the data and other is the link to the next node. These linked lists are known as single linked lists as they have only one pointer to point to the next node. If a linked list has two nodes, one is to point to the previous node and the other is to point to the next node, then the linked list is known as doubly linked list.

To use a linked list in C++ the following structure is to be declared:

typedef struct List
{long Data;
List* Next;
List ()
{Next=NULL;
Data=0;
}
};
typedef List* ListPtr.

The following code snippet is used to add a node.

void SLList::AddANode()
{ListPtr->Next = new List;
ListPtr=ListPtr->Next;
}

The following code snippet is used to traverse the list

void showList(ListPtr listPtr)
{
while(listPtr!=NULL) {
cout<<listPtr->Data;
}
return temp;
}
Post Your Answer

6 :: Given an unsorted linked list, and without using a temporary buffer, write a method that will delete any duplicates from the linked list?

Complexity: O(n^2) If you can sort the list in O(nlogn) then it will take O(nlogn).
Post Your Answer

7 :: How to reverse a singly linked list?

First off, in case you don't already know, the word 'iterative' when used in problems like these basically means that we use some sort of loop to solve the problem - whether it's a while loop, a for loop, or whatever type of loop you desire to use. We choose to use a while loop to come up with a solution.
Let's assume that we are going to start reversing the linked list starting from the very first node - the head node. What it basically comes down to is changing pointers from one node to the next so that the entire linked list becomes reversed. There is definitely a process - an algorithm - that we will want to follow in order to do that.
Post Your Answer

8 :: How to reverse a linked list iterative algorithm?

☼ The head node's next pointer should be set to NULL since the head will become the tail. This is an exception for the head node, and can be done outside the while loop. But, before we do this we will need a temp variable to point to the 2nd node (the node after the head node), because the only way to reference the 2nd node is through the head node's next pointer.
☼ The 2nd node (the node after the head node) should have it's own next pointer changed to point to the head node. This will reverse the order of the nodes. But, remember that the 2nd node's next pointer will at first be pointing to the 3rd node. This means that before we change the 2nd node's next pointer, we have to save a reference to the 3rd node otherwise we will have no way of referencing the 3rd node. So, we simply store a reference to the 3rd node in a variable before we change the 2nd node's next pointer.
☼ The 3rd node then becomes the "first" node in the while loop and we repeat the process of changing pointers described in step 2.
☼ Continue step 3 until we come across a node that has a next pointer set to NULL. When we do come across a NULL next pointer we just set the head node to point to the node that has the NULL next pointer. This node was previously the tail node, but is now the head node because we are reversing the linked list.
Post Your Answer

9 :: Explain reverse a linked list iterative solution in Java?

public reverseListIteratively (Node head)
{
if (head == NULL || head.next == NULL)
return; //empty or just one node in list

Node Second = head.next;

//store third node before we change
Node Third = Second.next;

//Second's next pointer
Second.next = head; //second now points to head
head.next = NULL; //change head pointer to NULL

//only two nodes, which we already reversed
if (Third == NULL)
return;

Node CurrentNode = Third;

Node PreviousNode = Second;

while (CurrentNode != NULL)
{
Node NextNode = CurrentNode.next;

CurrentNode.next = PreviousNode;

/* repeat the process, but have to reset
the PreviousNode and CurrentNode
*/

PreviousNode = CurrentNode;
CurrentNode = NextNode;
}

head = PreviousNode; //reset the head node
}
Post Your Answer

10 :: Tell me what should be done in the base case for this recursive problem?

Now, we know what the base case is and how to check for it, but what exactly should we be doing in this case? Well, we obviously want to have a return statement so that we can put a stop to the recursion. But, is there anything else that we should be doing here as well? It turns out that there is something else we need to do in the base case, because in the base case we are at the very end of the linked list. This means that because we are trying to reverse the list, we need to set the head pointer to point to the very last node.
Post Your Answer
Add New Question