Welcome to the Linked List Problems Master List! In this comprehensive guide, we'll explore various problems related to Linked Lists, a fundamental data structure in computer science. By the end of this tutorial, you'll be able to solve common problems using Linked Lists, enhancing your programming skills and preparing you for more complex algorithms. š”
A Linked List is a collection of data elements, called nodes, connected by links or references to each other. Unlike arrays, a Linked List doesn't have a fixed size; instead, you can dynamically add and remove nodes as needed.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
# Example usage:
linked_list = LinkedList()
linked_list.insert_at_beginning(5)
linked_list.insert_at_beginning(3)
linked_list.insert_at_beginning(2)class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_end(self, data):
if not self.head:
self.head = Node(data)
else:
current = self.head
while current.next:
current = current.next
current.next = Node(data)
# Example usage:
linked_list = LinkedList()
linked_list.insert_at_end(5)
linked_list.insert_at_end(3)
linked_list.insert_at_end(2)class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def get_length(self):
count = 0
current = self.head
while current:
count += 1
current = current.next
return count
# Example usage:
linked_list = LinkedList()
linked_list.insert_at_end(5)
linked_list.insert_at_end(3)
linked_list.insert_at_end(2)
print(linked_list.get_length()) # Output: 3class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def reverse(self):
prev_node = None
current = self.head
while current:
next_node = current.next
current.next = prev_node
prev_node = current
current = next_node
self.head = prev_node
# Example usage:
linked_list = LinkedList()
linked_list.insert_at_end(5)
linked_list.insert_at_end(3)
linked_list.insert_at_end(2)
linked_list.reverse()class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.cycle_detected = False
def has_cycle(self):
slow_ptr = self.head
fast_ptr = self.head
while fast_ptr and fast_ptr.next:
slow_ptr = slow_ptr.next
fast_ptr = fast_ptr.next.next
if slow_ptr == fast_ptr:
self.cycle_detected = True
return True
self.cycle_detected = False
return False
# Example usage:
linked_list = LinkedList()
linked_list.head = Node(1)
linked_list.head.next = Node(2)
linked_list.head.next.next = Node(3)
linked_list.head.next.next.next = linked_list.head # Creating a cycle
print(linked_list.has_cycle()) # Output: TrueWhat is the time complexity of inserting a node at the beginning of a Linked List?
Stay tuned for more Linked List problems and solutions! š”