Implement a Singly Linked List in JavaScript
Linked lists are fundamental data structures used in computer science. Understanding how to implement them is crucial for grasping concepts like dynamic memory allocation and efficient data manipulation. This challenge will guide you through building a basic singly linked list from scratch.
Problem Description
Your task is to implement a LinkedList class in JavaScript. A singly linked list is a linear data structure where elements are not stored at contiguous memory locations. Each element, called a node, contains two parts: a data field and a reference (or pointer) to the next node in the sequence.
You should implement the following methods for your LinkedList class:
constructor(): Initializes an empty linked list.insert(value): Adds a new node with the givenvalueto the end of the list.prepend(value): Adds a new node with the givenvalueto the beginning of the list.delete(value): Removes the first occurrence of a node with the givenvaluefrom the list.find(value): Returns the first node containing the givenvalue, ornullif not found.toArray(): Returns an array representation of the linked list.
The linked list should maintain a head property, which points to the first node in the list, and potentially a tail property for efficient appending.
Examples
Example 1:
// Initialize an empty list
const list = new LinkedList();
// Insert elements
list.insert(10);
list.insert(20);
list.prepend(5);
// Expected output of toArray()
[5, 10, 20]
Explanation:
-
The list starts empty.
-
insert(10): List becomes5 -> 10. -
insert(20): List becomes5 -> 10 -> 20. -
prepend(5): List becomes5 -> 10 -> 20. (Oops,prepend(5)was called afterinsert(20), so5would be at the front:5 -> 5 -> 10 -> 20. Let's correct the explanation based on the code execution order.)- Initialize:
head = null,tail = null list.insert(10):head = { value: 10, next: null },tail = headlist.insert(20):head = { value: 10, next: { value: 20, next: null } },tail = head.nextlist.prepend(5):head = { value: 5, next: { value: 10, next: { value: 20, next: null } } }list.toArray():[5, 10, 20]
- Initialize:
Example 2:
const list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.delete(2); // Remove the node with value 2
// Expected output of toArray()
[1, 3, 4]
Explanation:
The list initially is 1 -> 2 -> 3 -> 4. After deleting 2, the list becomes 1 -> 3 -> 4.
Example 3:
const list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
const foundNode = list.find(2);
// foundNode will be the node object: { value: 2, next: { value: 3, next: null } }
const notFoundNode = list.find(5);
// notFoundNode will be null
Explanation:
The find method should return the actual node object, not just its value, allowing further manipulation if needed.
Constraints
- The
LinkedListclass should be implemented using plain JavaScript. No external libraries are allowed. - Nodes should be simple objects with
valueandnextproperties. - The
insertmethod should add to the end of the list. - The
deletemethod should remove the first occurrence of the value. - The
findmethod should return the first node matching the value. - The
toArraymethod should return a standard JavaScript Array.
Notes
- Consider how to handle an empty list when performing operations like
deleteorfind. - For the
insertmethod, keeping track of thetailnode can significantly improve performance for appending. - Think about how to correctly update pointers when inserting at the beginning or deleting a node.
- The
Nodeclass or a factory function for creating nodes might be helpful, but not strictly required. You can simply create node objects directly.