Linked
Lists

Arrays Recap ...
Advantages:
1. Faster access to the elements.
2. Implementation is super simple.
Arrays Recap ...
Advantages:
1. Faster access to the elements.
2. Implementation is super simple.
Disadvantages:
1. Arrays are static structures (fixed size).
2. Expensive in Insertion and Deletion.
Linked List

A Linked list is a linear dynamic data structure
Linked List

Data
Pointer
Node
A Linked list is a linear dynamic data structure
Linked List

Data
Pointer
Node
A Linked list is a linear dynamic data structure
5
9
5
2
6
Linked List
Linked List

5 => 9 => 2 => 6 => 1=>
Linked List

5 => 9 => 2 => 6 => 1=>
Basic Operations
- Creating a Linked List
- Insertion
- Searching
- Deletion
- Printing a Linked List
Things to keep in mind
- Implementation can be tricky sometimes, carefully handle corner cases & None pointers
- Improper handling can lead to segmentation faults

A Linked List "Node"
class Node():
def __init__(self, data):
self.data = data
self.next = None
# Creating a Node
n = Node(10)
A Linked List Problems...
Insert in Linked List

Given a Linked List, write a function to insert

- At the Head of Linked List
- At the end of Linked List
- Somewhere in-between
Printing a Linked List

Given a Linked List, write a function to print the linked list.

Searching in a Linked List

Given a Linked List, write a function to search for an element in the linked list.

Recursive Search

Given a Linked List, write a function to search for an element in the linked list.

Deletion in Linked List

Given a Linked List, write a function to delete
- At the Head of Linked List
- At the end of Linked List

Reverse a Linked List

Given a Linked List, write a function to reverse the linked list.
Input
5 => 9 => 2 => 6 => 1=>
Output
1 => 6 => 2 => 9 => 5=>
5
9
5
2
6
1
Head
prev
next
curr
None
5
9
5
2
6
1
Head
prev
next
curr
None
5
9
5
2
6
1
Head
prev
curr
None
next
5
9
5
2
6
1
Head
prev
curr
None
next
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
None
prev
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
next
curr
5
9
5
2
6
1
Head
prev
None
None
next
curr
5
9
5
2
6
1
Head
prev
None
None
next
curr
5
9
5
2
6
1
Head
prev
None
None
next
curr
5
9
5
2
6
1
Head
prev
None
None
next
curr
5
9
5
2
6
1
Head
prev
None
None
next
curr

Recursive Reverse

Given a Linked List, write a function to reverse the linked list.
Input
5 => 9 => 2 => 6 => 1=>
Output
1 => 6 => 2 => 9 => 5=>
Middle Element

Given a linked list, write a function to find the middle node.
Input
Output
2
5
9
5
2
6
1
Input
Output
6
5
9
5
2
6
1
7
Merge 2 Sorted Linked Lists

Given two sorted linked lists, merge them into a new linked list.
Input
1-> 5 -> 7 -> 10 -> NULL
2-> 3 - > 6-> NULL
Output
1->2->3->5->6->7->10->NULL
1-> 5 -> 7 -> 10 -> NULL
2 -> 3 - > 6-> NULL
Merge Sort Linked List

Given a linked list, write a function to sort the linked list using Merge Sort.
Input
3->2->1->6->5->4->8->7->NULL
Output
1->2->3->4->5->6->7->8->NULL
Sum List

Input
First List: 5->6->3
Second List: 8->4->2
Output
Resultant list: 1->4->0->5
Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists
LRU Cache

A Real Life Data Structures Application


User (Client)
Cache
Disk


Requests Apple
Cache
Disk
CACHE MISS


Request
Cache
Disk



Request 2 - Guava
Cache
Disk



Cache
Disk




Request-3 Mango
Cache
Disk




Cache
Disk





Request-4 Orange
Cache
Disk





Request
Cache
Disk






Request Banana
Cache
Disk




Cache is Full!!


Request
Cache
Disk






Least Recently Used Item is removed from Cache


Request Mango Again
Cache
Disk




CACHE HIT


Cache - Mango exists, comes to the top again
Disk






Response is returned
Disk




LRU Cache

Implement a data structure, for LRU Cache Implementation, which supports following operations in O(1) time.
Operations
insert (Key, Value) getValue(key) getMostRecentKey()
LRU Cache ...contd

Also the class should have maxSize property which to set the size of cache. It represents the max number of key value pairs that can be stored inside cache at one time. If a key value pair is inserted in the cache when cache is full then the least recently used key-value pairs should be deleted and new one should be inserted. If the key already exists in the cache, then simple the value of the key can be updated with the new value.
Property
maxSize
How to Implement an
LRU Cache?
Python DSA - Linked List
By Prateek Narang
Python DSA - Linked List
- 12