DSAverse
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
Watch nodes link and unlink as you traverse, insert, and delete. Understand pointers and sequential access.
Each node contains a value and a next pointer. The last node points to NULL.
Color Legend
Use Insert, Delete, Search, or Access to begin visualization.
*Operations at head are O(1); others require traversal to position.
Create node → traverse to position → update pointers
Traverse → update prev.next to skip deleted node
Follow next pointers from head to target index
Linear traversal comparing values at each node
What is the time complexity of accessing an element at index k in a linked list?
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.size = 0
def insert(self, index, value):
if index < 0 or index > self.size:
raise IndexError("Index out of bounds")
new_node = Node(value)
if index == 0: # O(1) head insert
new_node.next = self.head
self.head = new_node
else: # O(n) traverse then link
current = self.head
for _ in range(index - 1):
current = current.next
new_node.next = current.next
current.next = new_node
self.size += 1
def delete(self, index):
if index < 0 or index >= self.size:
raise IndexError("Index out of bounds")
if index == 0:
value = self.head.value
self.head = self.head.next
else:
current = self.head
for _ in range(index - 1):
current = current.next
value = current.next.value
current.next = current.next.next
self.size -= 1
return value
def search(self, value): # O(n) linear scan
current, index = self.head, 0
while current:
if current.value == value:
return index
current, index = current.next, index + 1
return -1
def access(self, index): # O(n) traversal
current = self.head
for _ in range(index):
current = current.next
return current.value