LinkedList

Represents a linked list data structure.

Constructor

new LinkedList(valueopt)

Parameters:
NameTypeAttributesDescription
value*<optional>

The value to initialize the list with (optional).

Properties
NameTypeDescription
headLLNode

The first node in the list

tailLLNode

The last node in the list

sizeNumber

The number of nodes in the list

Methods

clear() → {LinkedList}

Clears the linked list, removing all elements.

Returns:

The current LinkedList instance.

Type: 
LinkedList

get(index, returnNodeopt) → {*}

Retrieves the value at the specified index in the linked list.

Parameters:
NameTypeAttributesDefaultDescription
indexnumber

The index of the value to retrieve.

returnNodeboolean<optional>
false

Specifies whether to return the Node instead of the value.

Returns:

The value at the specified index, or undefined if index is out of bounds.

Type: 
*
Example
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.

Parameters:
NameTypeDescription
indexnumber

The index at which to add the value.

value*

The value to add.

Returns:

True if the value was added successfully, false otherwise.

Type: 
boolean
Example
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.

Returns:

The removed value, or undefined if the list is empty.

Type: 
*
Example
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.

Returns:

The current LinkedList instance.

Type: 
LinkedList
Example
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.

Parameters:
NameTypeDescription
value*

The value to add.

Returns:

The current LinkedList instance.

Type: 
LinkedList
Example
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.

Parameters:
NameTypeDescription
indexnumber

The index at which to remove the value.

Returns:

The removed value, or undefined if the index is out of bounds.

Type: 
*
Example
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.

Returns:

The current LinkedList instance.

Type: 
LinkedList
Example
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.

Parameters:
NameTypeDescription
indexnumber

The index of the value to set.

value*

The new value to set.

Returns:

True if the value was set successfully, false otherwise.

Type: 
boolean
Example
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.

Returns:

The removed value, or undefined if the list is empty.

Type: 
*
Example
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.

Returns:

The current LinkedList instance.

Type: 
LinkedList
Example
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.

Returns:

An array containing the values of the linked list.

Type: 
Array
Example
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.

Parameters:
NameTypeDescription
value*

The value to add.

Returns:

The current LinkedList instance.

Type: 
LinkedList
Example
const ll = new LinkedList();
ll.push(10);
ll.push(20);
ll.push(30);

ll.unshift(0); // 0,10,20,30