How to Initialize a Linked List for Interview Problems in JavaScript (Leetcode) | Fast tutorial
How to Initialize a Linked List for Interview Problems in JavaScript (Leetcode) | Fast tutorial
In this tutorial, I show you how to initialize a linked list for interview problems that usually only require the head node. Hopefully by the end of this tutorial, you’ll know how to create a simple Node class from scratch that you can use to build out a linked list, and get the head node only, without a giant Linked List class.
To speed up the process of initializing a linked list, I start out with an array and iterate through array values to create a linked list. There are many different ways to create a linked list, and this is just one of them! I also show you how to print out your linked list just by passing in the head node.
I noticed that there are many tutorials out there that explain everything there is about linked lists, and those tutorials are great! But I could not find many about the bare minimum amount of code needed to start some of these linked lists problems in your local IDE, like VS Code, which is what I use. So for those curious and wanting to set up your linked lists outside of Leetcode, this should show you how to do it.
Thanks for watching!
Content
0.56 -> hi welcome back to my channel Carmelle Codes
and welcome if you're new! today we're going
4.8 -> to go over an interview type problem
which is how to initialize a linked list
10.72 -> for interview problems such
as those found on Leetcode or
15.52 -> Cracking the Coding Interview. this might be
obvious to everyone else but was not obvious to me
20.56 -> and i could not find a tutorial on just
the bare minimum amount of code needed for
27.12 -> these interview problems. so typically for
let's say a Leetcode problem you just get
33.52 -> this screen and you know that it's going to take
in the "head" and maybe another parameter but how
41.44 -> did Leetcode actually set up all of the linked
list data structure? well i'll show you right now
48.48 -> and this is how i do it. so first i define a class
node curly braces and inside i have a constructor
56.88 -> with two parameters, a value and next. so i
set this dot value equal to value and then i
62.48 -> do this dot next is equal to next question mark
next else no or colon null so this is saying if
69.52 -> there's a next value then set this dot next equal
to next, else, set it to null. i have yet to use
76.08 -> uh.. i have yet to pass in a next value but
i still continue to write it this way and i
81.6 -> don't know why so i'm just going to include
it in this tutorial! and how i initialize my
87.52 -> linked list usually is from an array so i do const
initializeLinkedListFromArray is equal to array
94.64 -> arrow notation um curly braces and
i say if array.length is equal to 0
100.16 -> return an empty array because there's no
linked list to make. else, we have an array
106.08 -> with elements so i'm going to let head equal
to new node array 0 and i say let current equal
113.68 -> head so for let i = 1, i < array.length, i++ then
i say let new node equal to new node array i and
124.96 -> then i do current.next is equal to the new node
that i've allocated somewhere and then i set a
131.6 -> current equal to current.next so i move current
one forward but i store head in the head variable
141.2 -> up above so at the end of this for loop i return
head and this is how i initialize my linked list.
152.24 -> the next function i usually implement is to
printLinkedList, which is equal to head and then
157.84 -> arrow and curly braces and i say let current equal
head and let print string equal empty string while
165.2 -> current print string plus equal current dot value
plus and then an arrow and a string and then i say
172.64 -> current is equal to current.next so i'm basically
iterating through the entire linked list and
177.44 -> adding all of those values to this variable called
print string and then i do console.log print
183.28 -> string so i just am able to print out uh the whole
string at the end. which is about O(n) runtime so
192.96 -> in order to use these functions that i just wrote
i do const head is equal to initialize linked
199.68 -> list from array and then i pass in an array
and then i do linked print linked list head
205.6 -> and that should print out the linked list
so that's kind of the sanity check to see
209.92 -> if you're following along if you implemented
those two correctly and then if you need
215.04 -> to write an interview problem you can just
pass in the head from initialize linked list
220.48 -> from array and that's basically it that is how
to initialize the link list from an array for
225.92 -> interview problems in javascript thank you
so much for watching have a good one, bye!