One Pager Cheat Sheet
- Understanding the fundamentals of
data structures
and when to appropriately use alinked list
is essential in software development. - Linked lists are linear data structures, meaning elements are traversed and constructed in a sequence and order, similar to a game of hopscotch.
- A
linked list
is a linear data structure composed ofnode
s that usepointers
to reference the next element in sequence and the starting and ending point of the list is marked by thehead
andtail
, respectively. - A
node
in alinked list
contains two essential parts: data members storing information and a link providing apointer
to the nextnode
. - A singly linked list consists of
nodes
that storedata
and areference
pointing to the next node. - A doubly linked list has nodes with a
data value
, a reference to theprevious
node, and a reference to thenext
node. - A
circular
linked list
allows efficient traversal of a full list beginning at any node. - A circular linked list consists of a set of
nodes
which link to one another in an unbrokencircular sequence
, with the tail node pointing back to the head node. - Linked lists are beneficial for shifting elements around, while arrays allow for faster item lookup in
constant
time. - A linked list is
dynamically size-able
due to itspointers
that reference thenext
memory address, making it easier to add and remove nodes compared to an array, which must be shifted around. - Reversing a
linked list
involves reassigning thenext
property of each node and iterating through the list with three pointers (current
,next
, andprev
) to achieve the desired reversal. - The
print(Node head)
functionloops
through thelinked list
starting from thehead
node and prints out the values of eachnode
to the console. - The
Node
class needs to be given the pointer to thenext
Node
in order to link them together in a linked list. - We use a
ptr
pointer to handle the special case when it isnull
, and then use atail
pointer to iterate over the originallinked list
and create a copy. - Deciding whether or not to use a
linked list
requires consideration of both the advantages and drawbacks, and one can further their understanding by implementing one.