A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node (tail).
Create a Linked List class - the linked list class should have the following properties & behavior:
- a head property.
- Upon instantiation, an empty Linked List should be created.
The linked list class should contain the following methods:
insert
Arguments: value Returns: nothing Adds a new node with that value to the head of the linked list with an O(1) Time performance.includes
Arguments: value Returns: Boolean Indicates whether that value exists as a Node’s value somewhere within the list.toString
Arguments: none Returns: a string representing all the values in the Linked List, formatted as: "{ a } -> { b } -> { c } -> NULL"append
Arguments: value Returns: nothing Adds a new node to the end of the linked listinsertAfter
Arguments: value, nextValue Returns: nothing Inserts a new node after the node with the specified valueinsertBefore
Arguments: value, nextValue Returns: nothing Inserts a new node before the node with the specified valuekthFromEnd
Arguments: k Returns: Int - the node’s value that is k places from the tail of the linked listreverse
Arguments: Linked List Returns: A copy of the Linked List reversed
Any exceptions or errors that come from your code should be semantic, capture-able errors. For example, rather than a default error thrown by your language, your code should raise/throw a custom, semantic error that describes what went wrong in calling the methods you wrote for this lab.
Follow your JavaScript standard naming conventions.
- Can successfully instantiate an empty linked list
- Can properly insert into the linked list
- The head property will properly point to the first node in the linked list
- Can properly insert multiple nodes into the linked list
- Will return true when finding a value within the linked list that exists
- Will return false when searching for a value in the linked list that does not exist
- Can properly return a collection of all the values that exist in the linked list
- Can successfully add a node to the end of the linked list
- Can successfully add multiple nodes to the end of a linked list
- Can successfully insert a node before a node located i the middle of a linked list
- Can successfully insert a node before the first node of a linked list
- Can successfully insert after a node in the middle of the linked list
- Can successfully insert a node after the last node of the linked list
Method | Time Complexity | Space Complexity |
---|---|---|
insert | O(1) | O(n) |
includes | O(n) | O(n) |
toString | O(n) | O(n) |
append | O(n) | O(n) |
insertAfter | O(n) | O(n) |
insertBefore | O(n) | O(n) |
nthFromEnd | O(n) | O(n) |
reverse | 0(n) | O(n) |
https://www.educative.io/edpresso/what-is-a-singly-linked-list