Linked Lists
Last updated
Was this helpful?
Last updated
Was this helpful?
A Linked List is a linear collection of data elements, called nodes, pointing to the neighbouring node by means of pointer.
Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node or previous node in the sequence.
β οΈ If you wanβt to learn more about linked-lists .
The most important part of a linked list is a node. It consist of two things β
Data
Pointers In Single Linked Lists there is only one pointer (next ) but in Double Linked lists there are two pointers (prev & next ). There can be more than two pointers also. It totally depends on your purpose of creating them.
this while loop is the one on which 75% of linkedLists dependsβ¦
Whenever we create an instance of our LinkedList
class, the constructor
executes and it create a head
node with both data and next as null
. Then we use its append
method to insert items into this list. In this post, we will go through the following methods/operations on Linked Lists β
Append
AppendAt
Remove
RemoveAt
Reverse
Swap
IsEmpty & Length
Traverse
Display
Search
and a lot moreβ¦ So, without further adoβ¦
Initial List
βοΈAll the code snippets you will see below are just Pseudo code.
Append Operation
AppendAt Operation
Remove Operation
RemoveAt Operation
Reverse Operation
It is easy to understand what we see instead of what we read. So, If the mechanism of this reverse
function is still unclear to you, I have made a gif
in order to show you how the execution goes.
pffffβ¦.finally I made it after spending 3 hours in just dragging and dropping.. but hope so it will help you :)
Swap Operation
IsEmpty & Length Operation
If you want to execute a function over each node, then you can use traverse
. Just pass the function as shown in the example β
Traverse Operation
Display Operation
Search Operation
There are still a lot of things that we can do with linked-lists. Never stop doing practice. Explore yourself and think of problems by yourself. The more you think of problems β the more you brain will create links with Data Structures.
Hackerrank
GeekForGeeks
LeetCode