Constructor
new LinkedList(valueopt)
Name | Type | Attributes | Description |
---|---|---|---|
value | * | <optional> | The value to initialize the list with (optional). |
Name | Type | Description |
---|---|---|
head | LLNode | The first node in the list |
tail | LLNode | The last node in the list |
size | Number | The number of nodes in the list |
Methods
clear() → {LinkedList}
Clears the linked list, removing all elements.
The current LinkedList instance.
- Type:
- LinkedList
get(index, returnNodeopt) → {*}
Retrieves the value at the specified index in the linked list.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
index | number | The index of the value to retrieve. | ||
returnNode | boolean | <optional> | false | Specifies whether to return the Node instead of the value. |
The value at the specified index, or undefined if index is out of bounds.
- Type:
- *
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.get(1); // 10
insert(index, value) → {boolean}
Adds a new value at the given index of the linked list.
Name | Type | Description |
---|---|---|
index | number | The index at which to add the value. |
value | * | The value to add. |
True if the value was added successfully, false otherwise.
- Type:
- boolean
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.insert(1,55); // 10,55,20,30
pop() → {*}
Removes the value from the end of the linked list.
The removed value, or undefined if the list is empty.
- Type:
- *
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.pop(); // 10,20
print() → {LinkedList}
Prints the values of the linked list.
The current LinkedList instance.
- Type:
- LinkedList
const ll = new LinkedList();
ll.push("Apple");
ll.push("Banana");
ll.push("Cherry");
ll.print(); // Apple, Banana, Cherry
push(value) → {LinkedList}
Adds a new value at the end of the linked list.
Name | Type | Description |
---|---|---|
value | * | The value to add. |
The current LinkedList instance.
- Type:
- LinkedList
const ll = new LinkedList();
ll.push(10); // 10
ll.push(20); // 10,20
ll.push(30); // 10,20,30
remove(index) → {*}
Removes the value at the given index of the linked list.
Name | Type | Description |
---|---|---|
index | number | The index at which to remove the value. |
The removed value, or undefined if the index is out of bounds.
- Type:
- *
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.shift(1); // 10,30
reverse() → {LinkedList}
Reverses the order of the linked list.
The current LinkedList instance.
- Type:
- LinkedList
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.reverse(); // 30,20,10
set(index, value) → {boolean}
Sets the value at the specified index in the linked list.
Name | Type | Description |
---|---|---|
index | number | The index of the value to set. |
value | * | The new value to set. |
True if the value was set successfully, false otherwise.
- Type:
- boolean
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.set(1, 0); // 0
shift() → {*}
Removes the value at the start of the linked list.
The removed value, or undefined if the list is empty.
- Type:
- *
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.shift(); // 20,30
sort() → {LinkedList}
Sorts the linked list in ascending order.
The current LinkedList instance.
- Type:
- LinkedList
const ll = new LinkedList([50,20,40,10,30]);
ll.sort(); // 10,20,30,40,50
toArray() → {Array}
Converts the linked list to an array.
An array containing the values of the linked list.
- Type:
- Array
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.toArray(); // [10,20,30]
unshift(value) → {LinkedList}
Adds a new value at the start of the linked list.
Name | Type | Description |
---|---|---|
value | * | The value to add. |
The current LinkedList instance.
- Type:
- LinkedList
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);
ll.unshift(0); // 0,10,20,30