Implementing a Singly Linked List in JavaScript
Linked lists are fundamental data structures used to store collections of elements in a sequential manner. They are particularly useful when the size of the collection is not known in advance or when frequent insertions and deletions are required. This challenge asks you to implement a singly linked list in JavaScript, demonstrating your understanding of node structures and list manipulation.
Problem Description
You are tasked with creating a LinkedList class in JavaScript. This class should support the following operations:
constructor(): Initializes an empty linked list.append(value): Adds a new node containing the givenvalueto the end of the list.prepend(value): Adds a new node containing the givenvalueto the beginning of the list.size(): Returns the number of nodes in the list.get(index): Returns the value of the node at the givenindex. Returnsundefinedif the index is out of bounds.delete(index): Removes the node at the givenindexfrom the list.print(): Returns a string representation of the linked list, with values separated by " -> ". Returns an empty string if the list is empty.
A node in the linked list should have two properties: value (the data stored in the node) and next (a reference to the next node in the list, or null if it's the last node).
Edge Cases to Consider:
- Empty list: Handle operations on an empty list gracefully.
- Invalid index:
get()anddelete()should handle out-of-bounds indices. - Deleting the last element: Ensure the list is properly updated after deleting the last node.
- Deleting the first element: Ensure the list's head is updated correctly.
Examples
Example 1:
Input:
let list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.print());
console.log(list.size());
console.log(list.get(1));
Output:
"1 -> 2 -> 3"
3
2
Explanation: The list is initialized, then three values are appended. The print method returns the string representation, size returns the number of nodes, and get(1) returns the value at index 1.
Example 2:
Input:
let list = new LinkedList();
list.prepend(1);
list.prepend(2);
list.prepend(3);
console.log(list.print());
console.log(list.size());
console.log(list.get(2));
Output:
"3 -> 2 -> 1"
3
1
Explanation: The list is initialized, then three values are prepended. The print method returns the string representation, size returns the number of nodes, and get(2) returns the value at index 2.
Example 3:
Input:
let list = new LinkedList();
list.append(1);
list.delete(0);
console.log(list.print());
console.log(list.size());
Output:
""
0
Explanation: A single element is appended, then deleted. The list becomes empty, so print returns an empty string and size returns 0.
Constraints
- The values stored in the linked list can be any JavaScript data type.
- The
indexparameter forget()anddelete()will be a non-negative integer. - The time complexity of
append()andprepend()should be O(1). - The time complexity of
size()should be O(1). - The time complexity of
get()anddelete()should be O(n), where n is the number of nodes in the list.
Notes
- Consider using a
headproperty in yourLinkedListclass to keep track of the first node. - Think about how to handle edge cases like an empty list or an invalid index.
- The
print()method is primarily for debugging and visualization; it doesn't need to be highly optimized. - Focus on clarity and correctness in your implementation. Good variable names and comments are encouraged.