Linked Lists

This is a brief overview of what a linked list is.

What is a linked list?

A linked list has a head (first node), a tail (last node), and a length. In order to get to anywhere in the linked list, you need to start at the head and go to the next one until you come to the node you were looking for. The last node (the tail) doesn’t have a pointer to another node but is null.

Linked List vs Array

Traversing Linked Lists

function traverse(linkedList){
// declare a variable that we will use to keep track of the current head
let head = linkedList
// An array to hold all the values of the linked list
let data = []
// while loop that keeps going until the current head is null which mean we reach the end
while (head !== null) {
data.push(head.val)
// change head to the next node
head = head.next
}
// return the array with the values
return data
}

That’s it! I hope this helps you understand this important data structure. Happy coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store