YouTube Icon

Interview Questions.

Top 100+ Linked List Interview Questions And Answers - May 31, 2020

fluid

Top 100+ Linked List Interview Questions And Answers

Question 1. How To Find Middle Element Of A Singly Linked List In One Pass?

Answer :

You need to clarify what does mean with the aid of one skip in this question. If Interviewer says that you can not loop two times and also you simply should use one loop, then you could use the two pointer technique to solving this hassle. In the 2 pointer technique, you have got two pointers, speedy and gradual. In every step, the quick pointer moves  nodes, whilst slow pointer simply steps one node. So, when rapid pointer will factor to the last node i.E. Where next node is null, the slow pointer could be pointing to the center node of the related list.

Question 2. How To Check If Linked List Contains Loop In Java? How To Find The Starting Node Of The Loop?

Answer :

This is another exciting related listing hassle which may be solved the usage of the two pointer technique mentioned in the first query. This is likewise known as tortoise and hare algorithm. Basically, you have  guidelines rapid and slow and that they circulate with one of a kind speed i.E. Rapid movements 2 notes in each new release and gradual movements one node. If linked listing includes cycle then at some point in time, both rapid and gradual pointer will meet and point to the equal node, if this did not take place and one of the pointer reaches the cease of linked list approach connected listing would not contain any loop.

C++ Interview Questions
Question three. How To Reverse A Linked List In Java?

Answer :

This might be the maximum popular connected listing interview question, that is requested to both junior programmers with 2 to three years of experience and senior developers containing four to 6 years of revel in. Some of you may think that is the simplest of linked list trouble however when you truly move doing it, you may be caught and many locations. The most effective method to fixing this problem is by way of the use of recursion because connected listing is a recursive data shape as shown in the answer article.

Question four. How To Reverse A Singly Linked List Without Recursion In Java?

Answer :

The previously linked list interview question turns into even more tough whilst the interviewer asked you to solve the hassle without recursion. You want to preserve reversing hyperlinks at the node until you reach the end, with a view to then become new head.

C++ Tutorial
Question 5. How Would You Remove A Node From A Doubly Linked List?

Answer :

This is one of the regularly asked related list interview questions, in most cases requested freshers and pc technology college graduates. In order to put off a node from the doubly connected list, you want to undergo that node and then change the hyperlinks so that it factors to the subsequent node. Removing nodes from head and tail is straightforward in linked list but disposing of a node from the middle of the linked list calls for you to journey to the node for this reason take O(n) time. If you want to learn greater approximately primary operations on linked list records structure, please read a great ebook on Data Structure and Algorithms e.G. Introduction to Algorithms by means of Thomas H. Carmen.

Data Warehousing Interview Questions
Question 6. Write A Program To Convert A Binary Tree Into A Doubly Linked List?

Answer :

This trouble is contrary of question 25 in which you need to write a program to transform a double connected list to the balanced binary tree.  The left and proper recommendations in nodes of a binary tree may be used as previous and subsequent tips respectively in transformed doubly connected listing. The order of nodes inside the doubly connected list have to be same as Inorder of the given Binary Tree. The first node of Inorder traversal (left most node in the binary tree) should be the head node of the doubly related list.

Question 7. How To Remove Duplicate Nodes In An Unsorted Linked List?

Answer :

This problem is similar in advance issues related to String and arrays i.E. Putting off reproduction elements in an array (see) or removing reproduction characters from given String (see right here). You want to put in writing a program to remove all replica nodes from an unsorted linked listing in Java. For example if the connected listing is 22->21->22->31->forty one->23->21 then your application need to convert the list to 22->21->31->41->23. This query is also given inside the famous Cracking the Coding Interview e-book so that you can study their solution as well.

Data Warehousing Tutorial Data Structures Interview Questions
Question eight. How To Find The Length Of A Singly Linked List In Java?

Answer :

This is one of the easiest linked list questions you could anticipate in an interview. That's why it's miles regularly asked on telephonic interviews. In order to locate the length of connected listing, you can iterate over connected list and maintain a rely of nodes until you attain the end of the connected list wherein subsequent node could be null. The cost of the counter is the length of connected list.

Question nine. Write Code To Print Out The Data Stored In Each Node In A Singly Linked List?

Answer :

This is every other only question which just exams whether you recognize related listing traversal or not. You can get the value from the node through accessing its value property, you just want to traverse through linked list, access each node and print price.

Computer Science Engineering Interview Questions
Question 10. Write A Program To Print A Linked List In Reverse Order? E.G. Print Linked List From Tail To Head?

Answer :

You can print nodes of related listing in opposite order by the use of Stack records shape in  steps:

Step 1: Traverse the related list from the head and placed the value of each node into Stack until you reach the last node. This will take O(n) time.

Step 2: Pop the factors out from the stack and print. This will take O(1) time.

Input: 1->2->3

Output: 3 2 1

Data Structures Tutorial
Question 11. How To Find The Kith Node From The End In A Singly Linked List?

Answer :

This is one of the complicated however frequently requested related listing questions. Some of you'll be questioning how do you locate kth node from quit, singly connected listing can most effective traverse in a single path and this is forward then how do you count nodes from the quit.
Well, you don't need to, you may nevertheless circulate ahead and count nodes from the quit? Actually, it really is the trick. You can use two suggestions to discover the Nth node from the end in a singly linked listing. They are known as fast and gradual points.
You begin sluggish pointer while the fast pointer reaches to the Kth node from begin e.G. If you have to find 3rdnode from the quit then you definitely start slow pointer whilst the fast pointer reaches to the 3rd node. This way, whilst your fast pointer reaches to the cease, your slow pointer will be on the third node from the cease.
C & Data Structures Interview Questions
Question 12. How To Delete Alternate Nodes Of A Linked List?

Answer :

You are given a Singly Linked List.  Starting from the second node delete all trade nodes of it. For instance, if the given related list is 1->four->eight->10->15 then your function should convert it to one->eight->15.

C++ Interview Questions
Question 13. What Is The Difference Between An Array And Linked List In Java?

Answer :

This is one of the frequently asked linked list questions about programming process interviews. There is a lot distinction among an array and linked listing however the most vital is how they may be saved into the memory vicinity. Array stores elements on the adjacent memory vicinity, even as related list shops them at scattered, which means looking is easy in an array and difficult in connected listing but adding and getting rid of an element from begin and quit is straightforward in connected listing. See here for more differences among array and linked list.

Data Structure & Algorithms Tutorial
Question 14. Difference Between Singly And Doubly Linked List In Java?

Answer :

The key distinction between a single and double related list records structure in java is that singly related listing most effective incorporates pointer to subsequent node, this means that you could only traverse in one path i.E. Forward, however doubly connected list consists of  factors, both preceding and next nodes, for this reason you can traverse to each forward and backward direction.

Question 15. How To Implement A Linked List Using Generics In Java?

Answer :

It's no longer smooth to enforce a related the use of generics in Java, specially if have now not written any parametric or customary elegance, however it's an awesome workout to get acquainted with both linked list facts structure as properly generics in Java.

Data Structure & Algorithms Interview Questions
Question 16. How To Insert A Node At The Beginning Of The List?

Answer :

Inserting a node at the start of the listing is probably the easiest of all operations. Let’s talk approximately what is worried right here referring to the diagram above. This involves growing a new node (with the brand new information, say int 10), making its link factor to the current first node pointed to by using head (records price 2) and lasting changing head to factor to this new node. Simple, proper.

Question 17. How To Insert A Node At The End Of The List?

Answer :

This case is a little bit extra developed. If you've got a tail pointer, it's far as smooth as putting at the beginning of the list. If you do no longer have a tail pointer, you'll have to create the new node, traverse the list until you reach the stop (i.E. The subsequent pointer is NULL) and then make that remaining node’s subsequent pointer point to the new node.

Advanced C# Interview Questions
Question 18. How Do You Traverse A Linked List In Java?

Answer :

There are a couple of ways to traverse a connected list in Java e.G. You could use conventional for, while, or do-even as loop and undergo the related listing until you attain the cease of the linked list. Alternatively, you could use improved for loop of Java 1.Five or Iterator to traverse via a connected listing in Java. From JDK 8 onwards, you may additionally use java.Util.Movement.Stream for traversing a related list.

Data Warehousing Interview Questions
Question 19. How Do You Find The Sum Of Two Linked List Using Stack In Java?

Answer :

This is a extraordinarily difficult related questions whilst you examine this to reversing a connected listing or including/doing away with elements from the linked listing. In order to calculate the sum of connected listing, you calculate the sum of values held at nodes inside the identical role, for example, you add values at the beginning node on each the connected listing to discover the primary node of resultant related listing. If the length of both linked listing isn't always equal you then most effective add elements from shorter linked listing and just replica values for final nodes from the lengthy list.

Question 20. How Do You Convert A Sorted Doubly Linked List To A Balanced Binary Search Tree In Java?

Answer :

This is one of the difficult related list questions you'll discover on interviews. You need to write down a application to transform a given doubly Linked, that is looked after in ascending order to assemble a Balanced Binary Search Tree which has equal the values because the given doubly related list. The project is usually improved by way of setting a restriction to construct the BST in-location i.E. No new node need to be allotted for tree conversion)

Input: A Doubly linked list 10  20 30  forty 50  60  70

Output: A balanced binary search tree BST

         forty

      /     

    20      60

   /          /   

 10   30  40   70

Advanced C++ Interview Questions
Question 21. How Do You Calculate The Sum Of Two Linked List Using Recursion In Java?

Answer :

This is another exciting connected listing primarily based algorithm query for Java programmers. You cannot use the java.Util.LinkdList class however you need to write your personal related list implementation in Java to clear up this hassle.

Question 22. How To Implement Lru Cache In Java Using Linked List?

Answer :

An LRU cache is the cache wherein you put off least recently used an element whilst the cache is full or approximately to fill. It's fantastically easy in Java if you are allowed to apply one of the Collection elegance e.G. You can use a LinkedHashMap to put in force LRU cache in Java, however you need to additionally put together how you may use a doubly related listing to create an LRU cache.

Question 23. How Do You Reverse Every Alternate K Nodes Of A Linked List In Java?

Answer :

This is another tough related listing set of rules query that is by and large requested to skilled programmers e.G. Programmer having 3 to six years of enjoy. You were given a singly related list and you need to write a characteristic to reverse each change ok nodes (in which ok is an input to the characteristic) in an efficient way. You also need to calculate the time and area complexity of your algorithm.

Example:

Inputs:   1->2->three->4->five->6->7->8->9->NULL and okay = three

Output:   three->2->1->4->five->6->9->eight->7->NULL.

C and C++ Interview Questions
Question 24. How Do Add Two Numbers Represented Using Linked List In Java?

Answer :

You have given two numbers represented with the aid of two related lists, write a characteristic that returns the sum of these two lists. The sum listing is linked list illustration of addition of  enter numbers. There are two restrictions to remedy this hassle i.E. You can't regulate the lists and you are not allowed to apply express more area. You can use recursion to clear up this hassle.

Input:

  First List: 1->2->three  // represents quantity 123

  Second List: nine->nine->9 //  represents number 999

Output:

 Resultant listing: 1->1->2->2  // represents range 1122

That's all about some of the often asked connected list primarily based coding questions from Programming Interviews. As I said, linked listing is one of the crucial records structure and you must have a good command over it, particularly in case you are preparing for Google or Amazon job interview. Once you clear up those problems, you may strive fixing questions given in Algorithm Design Manual with the aid of Steven S. Skiena. They are more difficult but can honestly enhance your information structure and set of rules competencies.

Data Structures Interview Questions
Question 25. What Is A Linked List?

Answer :

Linked list is an ordered set of records elements, every containing a hyperlink to its successor (and commonly its predecessor).

Question 26. How Many Pointers Are Required To Implement A Simple Linked List?

Answer :

You can discover normally three suggestions engaged:

A head pointer, pointing to the begin of the document.
A tail pointer, pointing at the remaining node of the listing. The key property within the closing node is that its next pointer points to nothing at all (NULL).
A pointer in every node, pointing to the subsequent node detail.
Question 27. How Many Types Of Linked Lists Are There?

Answer :

Singly linked listing, doubly related list, multiply connected listing, Circular Linked list.

Computer Science Engineering Interview Questions
Question 28. How To Represent A Linked List Node?

Answer :

The only representation of a linked listing node is wrapping the records and the hyperlink the usage of a typedef shape and giving the shape as a Node pointer that factors to the next node. An instance representation in C is

/*ll stands for connected list*/

typedef struct ll

    int records;

    struct ll *subsequent;

 Node;

Question 29. Describe The Steps To Insert Data At The Starting Of A Singly Linked List?

Answer :

Inserting facts at the beginning of the linked listing includes advent of a brand new node, placing the brand new node via assigning the head pointer to the brand new node subsequent pointer and updating the top pointer to the factor the brand new node. Consider placing a temp node to the first of listing

Node *head;

void InsertNodeAtFront(int records)

    /* 1. Create the brand new node*/

    Node *temp = new Node;

    temp->statistics = statistics;

    /* 2. Insert it at the first position*/

    temp->subsequent = head;

    /* 3. Replace the head to point to this new node*/

    head = temp;

Question 30. How To Insert A Node At The End Of Linked List?

Answer :

This case is a touch bit greater complex. It relies upon on your implementation. If you've got a tail pointer, it’s simple. In case you do now not have a tail pointer, you may should traverse the list until you reach the give up (i.E. The following pointer is NULL), then create a new node and make that remaining node’s subsequent pointer factor to the new node.

Void InsertNodeAtEnd(int data)

   /* 1. Create the new node*/

    Node *temp = new Node;

   temp->facts = statistics;

    temp->next = NULL;

    /* test if the listing is empty*/

    if (head == NULL)

    

        head = temp;

        go back;

    

    else

    

        /* 2. Traverse the list until the quit */

        Node *visitor = head;

        whilst (vacationer->next != NULL)

            traveler = tourist->subsequent;

       /* 3. Replace the last node to point to this new node */

        visitor->next = temp;

    

Question 31. How To Insert A Node In Random Location In The List?

Answer :

As above, you’d initial produce the new node. Currently if the placement is one or the list is empty, you’d insert it first of all function. Otherwise, you’d traverse the list till either you reach the required role or the list ends. Then you’d insert this new node. Inserting in the center is that the difficult case as you have were given to ensure you do the pointer assignment inside an appropriate order. First, you’d set the new nodes next pointer to the node before that the new node is being inserted. Then you’d set the node to the position to motive to the new node. Review the code under to get an idea.

Void InsertNode(int records, int position)

    /* 1. Create the new node */

    Node *temp = new Node;

    temp->data = statistics;

    temp->subsequent = NULL;

    /* check if the location to insert is first or the listing is empty */

    if ((position (head == NULL))

    

        // set the new node to point to head

        // because the listing won't be empty

        temp->subsequent = head;

        // factor head to the first node now

        head = temp;

        go back;

    

    else

    

        /* 2. Traverse to the desired function */

    Node *t = head;

int currPos = 2;

        even as ((currPos < position) && (t->subsequent != NULL))

        

            t = t->subsequent;

            currPos++;

        

        /* three. Now we're at the desired region */

        /* 4 first set the pointer for the new node */

        temp->next = t->subsequent;

        /* 5 now set the preceding node pointer */

        t->next = temp;

    

Question 32. How To Delete A Node From Linked List?

Answer :

The following are the stairs to delete node from the listing at the desired position.
Set the pinnacle to point to the node that head is pointing to.
Traverse to the favored role or until the listing ends; whichever comes first
You must factor the preceding node to the following node.
Question 33. How To Reverse A Singly Linked List?

Answer :

First, set a pointer (*modern) to factor to the primary node i.E. Present day=head.
Move beforehand until cutting-edge!=null (until the give up)
set another pointer (*subsequent) to point to the subsequent node i.E. Next=contemporary->next
save reference of *next in a transient variable (*result) i.E. Contemporary->subsequent=end result
switch the result price with contemporary i.E. End result=cutting-edge
And now switch the current price with next. I.E. Contemporary=next
go back result and repeat from step 2
A linked listing also can be reversed the use of recursion which gets rid of the use of a brief variable.
C & Data Structures Interview Questions
Question 34. Compare Linked Lists And Dynamic Arrays?

Answer :

A dynamic array is a information structure that allocates all factors contiguously in reminiscence, and keeps a rely of the existing quantity of elements. If the location reserved for the dynamic array is surpassed, it’s reallocated and traced, a high-priced operation.
Linked lists have many benefits over dynamic arrays. Insertion or deletion of an detail at a specific point of a listing, is a constant-time operation, whereas insertion in a dynamic array at random places might require transferring half of the elements on the common, and all of the factors inside the worst case.
Whereas one can delete an element from an array in consistent time with the aid of come what may marking its slot as vacant, this causes fragmentation that impedes the performance of iteration.
Question 35. What Is A Circular Linked List?

Answer :

In the ultimate node of a singly linear list, the hyperlink discipline regularly includes a null reference. A less not unusual convention is to make the ultimate node to factor to the primary node of the list; in this situation the list is said to be ‘circular’ or ‘circularly linked’.

Question 36. What Is The Difference Between Singly And Doubly Linked Lists?

Answer :

A doubly connected list whose nodes include three fields: an integer value and  links to other nodes one to point to the preceding node and different to point to the subsequent node. Whereas a singly connected list contains points most effective to the subsequent node.

Data Structure & Algorithms Interview Questions
Question 37. What Are The Applications That Use Linked Lists?

Answer :

Both stacks and queues are frequently applied the use of connected lists, other packages are bypass list, binary tree, unrolled connected listing, hash desk, heap, self-organizing list.

Question 38. How To Remove Loops In A Linked List (or) What Are Fast And Slow Pointers Used For?

Answer :

The nice answer runs in O(N) time and uses O(1) area. This approach makes use of two hints (one gradual pointer and one rapid pointer). The sluggish pointer traverses one node at a time, while the short pointer traverses two times as rapid because the first one. If the linked list has loop in it, sooner or later the quick and sluggish pointer could be on the same node. On the other hand, if the list has no loop, obviously the fast pointer will attain the stop of listing earlier than the sluggish pointer does. Hence we stumble on a loop.

Question 39. What Will You Prefer To Use A Singly Or A Doubly Linked Lists For Traversing Through A List Of Elements?

Answer :

Double-related lists require greater space in step with node than singly favored lists, and their standard operations along with insertion, deletion are more expensive; however they're frequently less difficult to manipulate because they allow speedy and easy sequential get admission to to the listing in each directions. On the alternative hand, doubly linked lists can not be used as continual data structures. So, for traversing through a list of node, doubly linked list would be a better desire.




CFG