is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.
You can use methods of LinkedList such as:
- get(position) - for getting a node from position
- push(data) - for adding new node to your List (to the its end)
- add(data, position) - to add a node to a specific position
- remove(position) - to remove node from specific position
- also u can use next methods: getHead(), isEmpty(), clear(), size().
To create a new List:
const linkedList = new LinkedList();Now linked list looks like:
LinkedList { head: null, length: 0 }To add some data to the end of List:
linkedList.push('node1');
linkedList.push('node3');Now linked list looks like:
LinkedList {
head: Node { data: 'node1', next: Node { data: 'node3', next: undefined } },
length: 2 }To push some data to a specific position:
linkedList.add('node2', 1);Now linked list looks like:
LinkedList {
head: Node { data: 'node1', next: Node { data: 'node2', next: [Object] } },
length: 3 }To remove the node from specific position:
linkedList.remove(1);Now linked list looks like:
LinkedList {
head: Node { data: 'node1', next: Node { data: 'node3', next: undefined } },
length: 2 }To get node from position:
LinkedList.get(1);This method will return the node from spec. position:
Node { data: 'node3', next: undefined }