๐ 28. How do you implement a circular linked list?
In a circular linked list: Last node โ points to head instead of NULL.
๐น Visualization
1 โ 2 โ 3
โ โ
โ โ โ โ
๐น Python Example
class Node:
def init(self, data):
self.data = data
self.next = None
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.next = third
third.next = head
๐น Uses
โ
Round-robin scheduling
โ
Multiplayer games
โ
Music playlists
โ
CPU scheduling
๐ 29. How do you split a list into equal parts?
๐น Approach
1. Count total nodes
2. Divide length
3. Break links carefully
๐น Example
1 โ 2 โ 3 โ 4 โ 5 โ 6
Split into 2 parts:
1 โ 2 โ 3
4 โ 5 โ 6
๐น Python Idea
length = count_nodes(head)
part_size = length // k
extra = length % k
Distribute remaining nodes one by one.
๐น Complexity
Time โ O(n)
Space โ O(1)
๐น Interview Tip
Frequently appears in partitioning problems.
๐ 30. How do you implement a doubly linked list?
A doubly linked list stores: prev pointer + next pointer
๐น Visualization
NULL โ 1 โ 2 โ 3 โ NULL
๐น Python Implementation
class Node:
def init(self, data):
self.data = data
self.prev = None
self.next = None
๐น Advantages
โ
Bidirectional traversal
โ
Easier deletion
โ
Efficient backtracking
๐น Disadvantages
โ More memory
โ Extra pointer management
๐น Real-World Uses
โ
Browser history
โ
Undo/redo
โ
Navigation systems
โ
Music players
๐น Complexity
Insert/Delete โ O(1)
Search โ O(n)
๐ฅ Double Tap โค๏ธ For Part-4
Post #2958
1.19K
- โค 2