Linked list interview questions and answers - freshers, experienced

Describe 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)

Explain 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.

Define 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.

Define 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.

Describe 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;
}
Data Structures Sorting - Answers to Sorting related interview questions
Data Structures Sorting - In this series, we have covered all about Sorting and answered the questions that might be asked during an interview.
Data structures stack and queue interview questions - stack and queue interview
Data Structures Stack and Queue - In this series, we have covered all about Stack and Queueand answered the questions that might be asked during an interview.
Data Structures Trees interview questions - freshers, experienced
Data Structures Trees interview questions for freshers and experienced - In this series, we have covered all about Trees and answered the questions that might be asked during an interview.
Post your comment
Discussion Board
Data Structures
i want to achieve knowledge of data_structure
Abdur Rashid 11-7-2015