Image for post

Hello everyone and welcome back to our blog. Today we are going to discus and learn how to write a new method for linked list. As always I would like to pay attention one thing it’s previous blogs. I will recommend to start with them.

Get last element in the list

Function → “getLast()”

Directions

Returns the node of the linked list.

Example

const list = new LinkedList();

list.insertFirst(‘a’)
list.insertFirst(‘b’)
list.getLast(); //returns node with data ‘a’

First of all, we need to check if there is any element, if it’s not we just return null. Then we create variable node where we point to the head(first element).We create while loop to move till the last element. If the next element is null, we return the node.

Image for post

We are using example that I was sharing with you above.

Image for post

Image for post

#react #javascript #linked-lists #algorithms #data-structures

JavaScript.Linked Lists. Get last element in the list. Clear the list.
1.85 GEEK