Linked List
Initialize the linked list
You can create an empty linked list or you can pass an array which will convert an array to an linked list.
import { LinkedList } from "quark-dsa";
const list = new LinkedList(); // returns an empty linked list.
or;
const list = new LinkedList([17, 2, 96, 55, 40]); // returns a linked list with array elements inserted sequentially.
Supported methods
Method | Description | Returns |
---|---|---|
constructListFromArray(itemsList) | Constructs a linked list from an array | returns the head of the linked list |
reverse() | Reverse the linked list. | returns the head of the reversed linked list |
length() | Calculates the length of the linked list. | returns the node count of the linked list |
addFirst(value) | Adds node to the first position of the linked list. | returns the head of the linked list |
addLast(value) | Adds node to the last position of the linked list. | returns the head of the linked list |
add(value, index) | Adds node to the given index of the linked list. If index is not given, it will insert node at last. | returns the head of the linked list |
remove(index) | Removes the node at given index of the linked list. If index is not given, it will remove node at first. | returns the head of the linked list |
getValue(index) | Returns the value of the node at the given index. | returns value of the node |
update(value, index) | Updates the value of the node at the given index. | returns the head of the linked list |
print() | Returns the value of the node separated by -> | returns the linked list in string format |
getMiddleNode() | Returns the middle node of the linked list. | returns the middle node |
getMiddleValue() | Returns the value of the middle node of the linked list. | returns the value of the middle node |
rotateRight(position) | Rotates the linked list to right by position times. If position is negative, it will rotate the linked list towards left. | returns the head of the rotated list |
rotateLeft(position) | Rotates the linked list to left by position times. If position is negative, it will rotate the linked list towards right. | returns the head of the rotated list |
concat(list) | Concatenates the second linked list into first one and return the first linked list. | returns the head of the first linked list |
Usage
import { LinkedList } from "quark-dsa";
const list = new LinkedList([17, 2, 96, 55, 40]); // returns a linked list with array elements inserted sequentially.
list.print(); // returns the value in the console log "17 -> 2 -> 96 -> 55 -> 40".
list.add(3); // inserts 3 at last of the linked list.
list.update(45, 3); // updates node of index 3 with value 45.
list.remove(5); // removes node at index 5;
list.reverse(); // reverse the list.
list.getMiddleValue(); // returns the middle value of the list, 96.
list.rotateRight(4); // rotates the list to 4 positions towards right.
list.rotateLeft(5); // rotates the list to 5 positions towards left.
const list2 = new LinkedList([89, 21, 63, 45]); // returns a linked list with array elements inserted sequentially.
list.concat(list2); // returns list with concatenated values of list2.