Linked List - Singly, Doubly, Circular
Linked list is extremely important topic for interviews being asked quite a lot. The two pointers
method is like the cycle detection stuff middle of linked list these are like some patterns very
famous patterns. link list basically says that it 's not going to be like continuous memory allocation
okay this thing will not be fixed instead try to break these boxes down into separate boxes like
boxes. Link list is efficient even though it is off one amortized or on an average constant time.
Linklist is not like an array of five items and what happens is that you try to add a sixth item in
arrow list. A linked list is a singly linked list. Every single item knows about the next item. Items
are not like continuous memory allocation. They're connected with each other using the pointer that
we 'll talk more about later it 's not the c plus pointer it's something else reference variable for
example.
Every single object is going to have this arrow that is pointing to and it 's pointing to what a node
type so node next is equal to what something like that this is what the representation will be of
every single box over here. This does not know when the list is ending list will only end when the
when you reach at last. head and tail it 's important to know how to work with just the head in the in
the linked list because tail value makes it easier for us. If tail was not provided how will you insert
in last n you would have to first find the last element traverse till the end whenever you find an item
that has a next equal to null it means you found the last item then just add add on to it. This as
linked list so we are actually creating internal linked list. If you want to see linked list in action like
the one that is already provided by java we you can just check this one link list of type integer list is
equal to new linked list you can create generic ones as well.
Every linked list is going to have this linked list contains this entire thing it has multiple multiple
nodes each node will be connected to each other using this this thing. Head and tail pointer are just
pointing to the nodes these are the reference variables pointing to this node that 's it created a new
node three. Head was initially what null because i did not assign it to anything you can see uh in the
you know when i just did i just said private node head that 's it that's it so i 'll just say head is equal
to node head will also be available over here but one thing remaining is tail. If there is no single
element provided if there was like an empty list so what would you do you create create create a
new one. Head is going to be moving forward so head is moving forward. Head is moving to the
new node the next of this node to head so i can say node. next is equal to head no problem now i 'm
going to point the head to the newly established node because head always points to the first node
like this. This is actually wrong why i 'll tell you later on this is wrong.
Head always supposed is supposed to be at the first element but after you started printing it became
in the last that 's why you never move head like this until unless you 're changing the structure of
the link list. When i say temp is equal to head it basically is pointing to the same object that head is
pointed to so when you do temp dot next it means the temp easy equal equal to means temp itself is
changing to something else. Next is a reference variable that is going to point to the object that i
provided okay so i can say while temp is not equal to null what do i do again say print temp dot
value plus something like this and then i say okay you have printing correct one move ahead so now
temp is equal to the next value of temp temp temp dot next. internally internally internally it should
look something like this what does this mean now see what how to do it so what does it mean it
means it means that tails next should be equal to the node that i have just created so tail dot next is
equal to node. is the head this is the tail all right cool so now you want to insert a new item you
seven insert 17 or insert 17.
Linked list is extremely important topic for interviews being asked quite a lot. The two pointers
method is like the cycle detection stuff middle of linked list these are like some patterns very
famous patterns. link list basically says that it 's not going to be like continuous memory allocation
okay this thing will not be fixed instead try to break these boxes down into separate boxes like
boxes. Link list is efficient even though it is off one amortized or on an average constant time.
Linklist is not like an array of five items and what happens is that you try to add a sixth item in
arrow list. A linked list is a singly linked list. Every single item knows about the next item. Items
are not like continuous memory allocation. They're connected with each other using the pointer that
we 'll talk more about later it 's not the c plus pointer it's something else reference variable for
example.
Every single object is going to have this arrow that is pointing to and it 's pointing to what a node
type so node next is equal to what something like that this is what the representation will be of
every single box over here. This does not know when the list is ending list will only end when the
when you reach at last. head and tail it 's important to know how to work with just the head in the in
the linked list because tail value makes it easier for us. If tail was not provided how will you insert
in last n you would have to first find the last element traverse till the end whenever you find an item
that has a next equal to null it means you found the last item then just add add on to it. This as
linked list so we are actually creating internal linked list. If you want to see linked list in action like
the one that is already provided by java we you can just check this one link list of type integer list is
equal to new linked list you can create generic ones as well.
Every linked list is going to have this linked list contains this entire thing it has multiple multiple
nodes each node will be connected to each other using this this thing. Head and tail pointer are just
pointing to the nodes these are the reference variables pointing to this node that 's it created a new
node three. Head was initially what null because i did not assign it to anything you can see uh in the
you know when i just did i just said private node head that 's it that's it so i 'll just say head is equal
to node head will also be available over here but one thing remaining is tail. If there is no single
element provided if there was like an empty list so what would you do you create create create a
new one. Head is going to be moving forward so head is moving forward. Head is moving to the
new node the next of this node to head so i can say node. next is equal to head no problem now i 'm
going to point the head to the newly established node because head always points to the first node
like this. This is actually wrong why i 'll tell you later on this is wrong.
Head always supposed is supposed to be at the first element but after you started printing it became
in the last that 's why you never move head like this until unless you 're changing the structure of
the link list. When i say temp is equal to head it basically is pointing to the same object that head is
pointed to so when you do temp dot next it means the temp easy equal equal to means temp itself is
changing to something else. Next is a reference variable that is going to point to the object that i
provided okay so i can say while temp is not equal to null what do i do again say print temp dot
value plus something like this and then i say okay you have printing correct one move ahead so now
temp is equal to the next value of temp temp temp dot next. internally internally internally it should
look something like this what does this mean now see what how to do it so what does it mean it
means it means that tails next should be equal to the node that i have just created so tail dot next is
equal to node. is the head this is the tail all right cool so now you want to insert a new item you
seven insert 17 or insert 17.