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

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. Write a JavaScript program to create and display Singly Linked List. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

4. Write a JavaScript program to insert a new node at any position of a Singly Linked List. Go to the editor

Click me to see the solution

5. Write a JavaScript program to insert a new node at the beginning of a Singly Linked List. Go to the editor

Click me to see the solution

6. Write a JavaScript program to insert a new node at the end of a Singly Linked List. Go to the editor

Click me to see the solution

7. Write a JavaScript program to get a node in an existing singly linked list. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

12. Write a JavaScript program that calculates the size of a Singly Linked list. Go to the editor

Click me to see the solution

13. Write a JavaScript program that removes the first element from a Singly Linked list. Go to the editor

Click me to see the solution

14. Write a JavaScript program that removes the tail element from a Singly Linked list. Go to the editor

Click me to see the solution

15. Write a JavaScript program to convert a Singly Linked list into an array and returns it. Go to the editor

Click me to see the solution

16. Write a JavaScript program to convert a Singly Linked list into a string and returns it. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

1. Write a JavaScript program to create and display Doubly Linked Lists. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

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.

JavaScript: Tips of the Day

Shorten an array using its length property

A great way of shortening an array is by redefining its length property.

let array = [0, 1, 2, 3, 4, 5, 6, 6, 8, 9]
array.length = 4
// Result: [0, 1, 2, 3]

Important to know though is that this is a destructive way of changing the array. This means you lose all the other values that used to be in the array.

Ref: https://bit.ly/2LBj213