w3resource

JavaScript Linked List - Exercises, Practice, Solution

JavaScript Data Structures: Singly Linked List [18 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

From Wikipedia,
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. A drawback of linked lists is that access time is linear. Faster access, such as random access, is not feasible. Arrays have better cache locality compared to linked lists.

1. Create and display a SLL

Write a JavaScript program to create and display a Singly Linked List.

Click me to see the solution

2. Create a SLL of n nodes and display in reverse

Write a JavaScript program to create a singly linked list of n nodes and display it in reverse order.

Click me to see the solution

3. Count the number of nodes in a SLL

Write a JavaScript program to create a singly linked list of n nodes and count the number of nodes.

Click me to see the solution

4. Insert a node at any position in a SLL

Write a JavaScript program to insert a node at any position in a Singly Linked List.

Click me to see the solution

5. Insert a node at the beginning of a SLL

Write a JavaScript program to insert a node at the beginning of a Singly Linked List.

Click me to see the solution

6. Insert a node at the end of a SLL

Write a JavaScript program to insert a node at the end of a Singly Linked List.

Click me to see the solution

7. Get a node in a SLL

Write a JavaScript program to get a node in an existing singly linked list.

Click me to see the solution

8. Find the first index that matches a given element

Write a JavaScript program to find the first index that matches a given element. Return -1 for no matching.

Click me to see the solution

9. Check if a SLL is empty

Write a JavaScript program to check whether a single linked list is empty or not. Return true otherwise false.

Click me to see the solution

10. Empty a SLL

Write a JavaScript program to empty a singly linked list by pointing the head towards null.

Click me to see the solution

11. Remove the node from a SLL at the specified index

Write a JavaScript program that removes the node from the singly linked list at the specified index.

Click me to see the solution

12. Calculate the size of a SLL

Write a JavaScript program that calculates the size of a Singly Linked list.

Click me to see the solution

13. Remove the first element from a SLL

Write a JavaScript program that removes the first element from a Singly Linked list.

Click me to see the solution

14. Remove the tail element from a SLL

Write a JavaScript program that removes the tail element from a Singly Linked list.

Click me to see the solution

15. Convert a SLL into an array

Write a JavaScript program to convert a Singly Linked list into an array.

Click me to see the solution

16. Convert a SLL into a string

Write a JavaScript program to convert a Singly Linked list into a string.

Click me to see the solution

17. Get the index of an element in a SLL

Write a JavaScript program to get the index of an element in a Singly Linked list.

Click me to see the solution

18. Check if an element is present in a SLL

Write a JavaScript program to check if an element is present in the Singly Linked list.

Click me to see the solution

JavaScript Data Structures: Doubly Linked List [17 exercises with solution]

1. Create and display a DLL

Write a JavaScript program to create and display Doubly Linked Lists.

Click me to see the solution

2. Create a DLL of n nodes and count the number of nodes

Write a JavaScript program to create a Doubly Linked Lists of n nodes and count the number of nodes.

Click me to see the solution

3. Check if a DLL is empty

Write a JavaScript program to check whether a Doubly Linked Lists is empty or not. Return true otherwise false.

Click me to see the solution

4. Get the head and tail of a DLL

Write a JavaScript program to get the head and tail of a Doubly Linked Lists.

Click me to see the solution

5. Insert a new node at any position in a DLL

Write a JavaScript program to insert a new node at any position of a Doubly Linked List.

Click me to see the solution

6. Insert a new node at the beginning of a DLL

Write a JavaScript program to insert a new node at the beginning of a Doubly Linked List.

Click me to see the solution

7. Insert a new node at the end of a DLL

Write a JavaScript program to insert a new node at the end of a Doubly Linked List.

Click me to see the solution

8. Get the value of a node at a given position in a DLL

Write a JavaScript program to get the value of a node at a given position in a Doubly Linked List.

Click me to see the solution

9. Create a DLL of n nodes and display in reverse

Write a JavaScript program to create a Doubly Linked lists of n nodes and display it in reverse order.

Click me to see the solution

10. Convert a DLL into an array

Write a JavaScript program to convert a Doubly Linked lists into an array and returns it.

Click me to see the solution

11. Convert a DLL into a string

Write a JavaScript program to convert a Doubly Linked List into a string and return it.

Click me to see the solution

12. Get the index of an element in a DLL

Write a JavaScript program to get the index of an element in a Doubly Linked lists.

Click me to see the solution

13. Check if an element is present in a DLL

Write a JavaScript program to check if an element is present in a Doubly Linked lists.

Click me to see the solution

14. Remove the node from a DLL at the specified index

Write a JavaScript program that removes the node from the Doubly linked lists at the specified index.

Click me to see the solution

15. Remove the head element from a DLL

Write a JavaScript program that removes the head element from a doubly Linked lists.

Click me to see the solution

16. Remove the tail element from a DLL

Write a JavaScript program that removes the tail element from a doubly Linked lists.

Click me to see the solution

17. Update the value of a node at a specific index in a DLL

Write a JavaScript program that updates the value of a node at a specific index in a Doubly Linked list.

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Live Demo:

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.