Linked List: Frequently used Operations

🔥 Interview Questions with Beginner Friendly Explanations and Pseudo Code 🔥

👇
0️⃣ Basics

✪ What is a Linked List?

• a LINEAR data structure
• its elements may not be in contiguous memory location
• each element has a reference to its next element

✪ What is an element in a Linked List popularly called?
A Node.

++
✪ What is the structure of a Node in Linked List?

A node has 2 parts.

• First one contains the value
• Second one contains the reference (or, pointer) to the next node. For the last node this is NULL as it doesn't point to any other node.

++
✪ What extra is required?

• A reference to start traversal with.
• It's convenient to have the reference of the very first node.
• This reference is referred as Head.

✪ Pseudo Code for Node and, Head ✪

Node {
Value,
next: Node
}
Head = NULL

✪ ✪ ✪ ✪ ✪ ✪ ✪ ✪
1️⃣ Traverse through Nodes

✪ Why do we need to traverse?
Elements in Linked List are not stored contiguously. Then how to reach at an element? For that we need to traverse.

✪ Where to start?
Start from Head
If Head is NULL, Linked List is empty hence no traversal required.

++
✪ How to traverse from one node to another?
Using "next" reference of a node.

✪ Where to end?
When "next" reference of a node is NULL, it means that node doesn't point any other node. So, that's the end node in the linked list. We will stop there.
2️⃣ IsEmpty

If Head is NULL, that means there are no elements in the linked list. Hence it is empty.

✪ Pseudo Code ✪

Return Head is NULL

✪✪✪✪✪✪✪✪✪✪
3️⃣ Size

Traverse through the linked list to find out the size (number of nodes in the list)

✪ Pseudo Code ✪

size = 0
curr = Head

while curr is not NULL
curr = curr->next
size++

Return size

✪✪✪✪✪✪✪✪✪✪
4️⃣.1️⃣ Get Front Node

Head has the reference to the very first node aka the "Front Node".

But when Head is NULL, linked list is empty and hence there is no front node present.

✪ Pseudo Code ✪

If Head is NULL
Throw Error
Return Head
4️⃣.2️⃣ Get End Node

Starting from Head, we have to traverse till the end node.

✪ Pseudo Code ✪

If Head is NULL
Throw Error

current = Head
while current->next is not NULL
current = current->next

Return current
✪ ✪ ✪ ✪ ✪ ✪ ✪ ✪ ✪ ✪
4️⃣.3️⃣ Get Any Node

Let's say the task is to get a node at kth index where 0th index means the very first node.

✪ Pseudo Code ✪

current = Head
i = 0
while i < k and current is not NULL
current = current->next
i++

If i < 0 or current is NULL
Throw Error

Return current
5️⃣.1️⃣ Insert at the Front

Given: "node", which will be inserted at the front

It means after insertion,
• the current Head node should be the 2nd node
• the "node" should be the Head node

✪ Pseudo Code ✪

If node is NULL
Throw Error

node->next = Head
Head = node

✪✪✪
5️⃣.2️⃣ Insert at the Last

Given:
• "node", which will be inserted at the last
• "last", which is current last node

It means after insertion,
• next of "last" node should point to "node"

✪ Pseudo Code ✪
If last is NULL
Throw Error

last->next = node

✪✪✪✪✪✪✪✪✪
5️⃣.3️⃣ Insert at any position

Given:
• "node", which will be inserted at kth position
• "prev", which is the node at (k-1)th position [Use 4️⃣.3️⃣ to get it)

It means after insertion,
• next of "node" should point to next of "prev"
• next of "prev" should point to "node"

++
✪ Pseudo Code ✪
If node is NULL or prev is NULL
Throw Error

node->next = prev->next
prev->next = node

✪✪✪✪✪✪✪✪✪
6️⃣.1️⃣ Delete from Front

After deletion,
• the current Head node should refer to the current 2nd node
• the "next" of 1st node should be NULL

✪ Pseudo Code ✪

If Head is NULL
Throw Error

temp = Head
Head = Head->next
temp->next = NULL

✪✪✪
6️⃣.2️⃣ Delete from Last

Given:
• "prevToLast", which is the previous to current last node

It means after deletion,
• next of "prevToLast" node should be NULL and effectively becomes the last node.

✪ Pseudo Code ✪
If prevToLast is NULL
Throw Error

prevToLast->next = NULL
6️⃣.3️⃣ Delete from any position

Given:
• "prev", which is the node at (k-1)th position [Use 4️⃣.3️⃣ to get it)

It means after deletion,
• next of "prev" should point to next of next of "prev"

++
✪ Pseudo Code ✪
If prev is NULL or prev->next is NULL
Throw Error

temp = prev->next
prev->next = temp->next
temp->next = NULL

✪✪✪✪✪✪✪✪✪
7️⃣ Space & Time Complexity

IsEmpty: O(1),N/A
Size: O(n), N/A
GetFrontNode: O(1), N/A
GetEndNode: O(n), N/A
GetAnyNode: O(n), N/A
InsertAtFront: O(1), N/A
InsertAtEnd: O(1), N/A
InsertAtAny: O(1), N/A
DeleteFromFront: O(1), O(1)
DeleteFromEnd: O(1), O(1)
DeleteFromAny: O(1), O(1)

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Swapna Kumar Panda ✨

Swapna Kumar Panda ✨ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @swapnakpanda

27 Oct
JavaScript CookBook: 7️⃣ Recipes for this Week

Recipes

1️⃣ Complex String Formation
2️⃣ String Conversions
3️⃣ Default Value Assign
4️⃣ Default Function Parameters
5️⃣ Checking properties of a possibly Nullish Object
6️⃣ Fetching Values from an Object
7️⃣ Function Argument List
Recipe 1️⃣

✪ What to Cook?
Complex String Formation

✪ Ingredient
`` (String Template Literal)

✪ Cooking Process
You were cooking like this (tedious):

→ 'Hello ' + fName + ' ' + lName

Instead cook like this:

→`Hello ${fName} ${lName}'
Read 9 tweets
26 Oct
🔥 Resources to learn Data Structures and Algorithms

1️⃣ Twitter Threads

I post almost regular threads on DSA. Check out those 👇
twitter.com/i/events/14502…
2️⃣ Blogs/Articles

1️⃣ Swapna's blog is coming soon. It will include from simple to complex topics on DSA. It will also help you getting through how to approach for solving the problem. It should be your one-stop to get familiar with DSA.

Some more to look for 👇
Read 9 tweets
25 Oct
Data Structures and Algorithms (DSA)

🔥 Frequently Asked Interview Questions 🔥

I am soon going to start my blog. All the questions featured here will be answered in detail over there. I will post answers for some of these in Twitter platform as well. Questions will be added with time. Stay tuned.

Now, let's go through the questions.

1️⃣ Basics

✪ What is an algorithm?
✪ What is time complexity?
✪ What is space complexity?
✪ What is a Data Structure?
✪ What are types of Data Structures? (Linear/Non-linear)
✪ What are different operations that can be performed on different data structures?
Read 14 tweets
23 Oct
Tree, Binary Tree and Binary Search Tree

Know the difference. 👇
0️⃣ Introduction to a Tree

✪ A Tree is a data structure where each element is called as a "Node".

✪ Unlike a Linked List, a Node in a Tree can point many (child) nodes.

✪ The first node from which traversal begins is called the Root Node.

++
✪ Each child node can be root of a separate tree. Each such trees are known as "Subtrees" of a given node.

✪ The number of parents, a node can have is called its "In-Degree" and it is always 1.

✪ The number of children, a node can have is called its "Out-Degree".
Read 7 tweets
22 Oct
Difference between an Array and a Linked List

These differences will make you grasp the fundamentals of both Array and Linked List really quick.

And if you are preparing for any interviews, it would definitely help you.

Let's explore 👇
1️⃣ Storage
2️⃣ Size
3️⃣ Access of Elements
4️⃣ Insertion/Deletion of Elements
5️⃣ Search for Elements
6️⃣ Memory Allocation
7️⃣ Memory Usage
8️⃣ Memory Utilisation
9️⃣ Use case

Read 12 tweets
22 Oct
All you would like to know about

✪ Array Data Structure
✪ Array Operations
✪ Array Based Problems

* Time and Space Complexities covered

DSA Series: 🧵 👇
We will cover

1️⃣ Introduction
2️⃣ Array Size
3️⃣ Array Index
4️⃣ Array Dimension
5️⃣ Array Operations
6️⃣ Other Array-based Data Structures
7️⃣ Some Array-based Problems
1️⃣ Introduction

An array is a "collection of items" stored at contiguous memory locations.

Syntax: All items are written comma-separated inside square brackets.

Example: [1, 2, 3, 4, 5]

There are total 5 items in the array. 1 is the first item, 2 is the 2nd and 5 is the last.
Read 22 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(