Linked List Problems Master List šŸŽÆ

beginner
21 min

Linked List Problems Master List šŸŽÆ

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. šŸ’”

What is a Linked List? šŸ“

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.

Basic Linked List Operations šŸ“

  • Insertion: Adding a new node to the Linked List
  • Deletion: Removing an existing node from the Linked List
  • Search: Finding a specific node in the Linked List
  • Traversal: Visiting each node in the Linked List sequentially

Linked List Problems šŸŽÆ

Problem 1: Inserting a Node at the Beginning of a Linked List šŸ’”

python
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)

Problem 2: Inserting a Node at the End of a Linked List šŸ’”

python
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)

Problem 3: Finding the Length of a Linked List šŸ’”

python
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: 3

Problem 4: Reversing a Linked List šŸ’”

python
class 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()

Problem 5: Detecting a Linked List Cycle šŸ’”

python
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: True

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the time complexity of inserting a node at the beginning of a Linked List?

Stay tuned for more Linked List problems and solutions! šŸ’”