Theoretical Foundation of Linked Lists

Fundamentals of Linked List
Learn the fundamentals of linked lists, including types, memory storage, operations, and performance comparisons with arrays. Discover how linked lists enable dynamic memory management, allow O(1) insertions and deletions, and are ideal for handling variable-sized data with frequent modifications.

This article introduces the following points:

  • The main types of linked lists are singly linked list, doubly linked list, and circular linked list.
  • Storage method of linked lists: the nodes of a linked list are stored in memory separately and connected by pointers.
  • How to perform CRUD (Create, Read, Update, Delete) operations on linked lists.
  • Performance analysis of arrays and linked lists in different scenarios.

Classic Linked List Problems

Dummy Head Node

Remove Linked List Elements
Learn three efficient approaches to remove nodes with a specific value from a linked list in Python. Explore direct removal, dummy head node, and recursive solutions. Complete with time/space complexity analysis and detailed examples. Perfect for coding interviews and data structure mastery.

One major issue with linked lists is that to operate on the current node, you must find the previous node first. This creates an awkward situation for the head node since there is no previous node.

Each time we deal with the head node's situation, it has to be handled separately, so using a dummy head node can solve this problem.

In this post, I've provided code examples with and without a dummy head node. By comparing them, everyone will notice the benefits of using a dummy head.

Basic Operations of Linked Lists

Design Linked List
Learn how to implement a custom linked list data structure in Python, covering both singly and doubly linked list variations. Master essential operations like insertion, deletion, and traversal with practical examples and optimal solutions for coding interviews.

In the above post:

We practiced the five common operations on linked lists by designing a linked list.

This is a very good problem to practice basic operations on linked lists, assessing:

  • Obtaining the value of the node at index in the linked list
  • Inserting a node at the front of the linked list
  • Inserting a node at the end of the linked list
  • Inserting a node before the node at index in the linked list
  • Deleting the value of the node at index in the linked list

One could say that after solving this problem, you have acquired a grasp of basic operations with linked lists and no longer need to worry about understanding insertions, deletions, modifications, and queries related to them.

Here I still used the technique of employing a dummy head node; you can review this when looking over your code.

Reverse Linked List

Reverse Linked List
Master three different approaches to solving LeetCode’s Reverse Linked List problem (#206). Learn the efficient two-pointer technique, recursive method, and stack-based solution with detailed explanations and Python implementations. Perfect for coding interviews and data structure practice.

In the above post:

It explains how to reverse a linked list.

Since the code for reversing a linked list is relatively simple, some students might have memorized it directly, but they still tend to make mistakes when writing it out.

Reversing a linked list is a high-frequency interview question and tests the interviewee's proficiency with linked list operations.

In the post, I provided two methods of reversal: iterative method and recursive method.

I suggest that everyone first thoroughly understand the iterative method before looking at the recursive method, as recursion can be quite convoluted; if you haven't mastered iteration yet, you'll likely struggle with recursion as well.

Delete the N-th node from the end 

Remove Nth Node From End of List
Learn how to remove the nth node from the end of a linked list with an efficient two-pointer approach in Python. This guide covers the problem definition, example solutions, and a clear step-by-step breakdown using a dummy head node to simplify edge cases. Perfect for coding interview prep!

We use a virtual head node and the two-pointer method to remove the N-th node from the end of a linked list.

Intersection of Linked Lists

Linked List Intersection
Find the intersection of two linked lists efficiently. This guide explains a method using dual pointers to detect the starting node of intersection, ensuring minimal traversal. Perfect for programmers tackling linked list problems in Python.

Intersection of Linked Lists uses two pointers to find the intersection point of two linked lists (where references are exactly identical, i.e., memory addresses are exactly identical).

Circular Linked List

Linked List Cycle II
Master the Linked List Cycle II problem with this comprehensive guide. Learn how to detect cycles in linked lists and find their entry points using two efficient approaches: the Floyd’s Tortoise and Hare algorithm and the set-based method. Includes detailed explanations and Python solutions

It explains how to find a loop in a linked list and how to locate its entry point. This problem can be considered one of the more difficult problems regarding linked lists. However, the code is quite concise, mainly due to some mathematical proofs.

Summary

Linked List Basics

  • A linked list consists of nodes where each node stores:
    • Data
    • A pointer/reference to the next node
  • Types of linked lists:
    • Singly linked list: One-directional links
    • Doubly linked list: Links in both forward and backward directions
    • Circular linked list: The last node links back to the first node

Linked List Operations

  • Basic Operations:
    • Traversal: Visiting each node in sequence
    • Insertion: Adding a node at a specific position
    • Deletion: Removing a node from a specific position
    • Searching: Finding a node with a given value
  • Advanced Operations:
    • Reverse a linked list:
      • Iteratively adjust the pointers to reverse the direction of links
    • Swap two adjacent nodes:
      • Use temporary pointers to adjust links
      • Time complexity: O(n), Space complexity: O(1)
    • Remove the N-th node from the end:
      • Use a two-pointer approach
      • Maintain a gap of n nodes between pointers

Cycle Detection in Linked Lists

  • Problem: Detect if a linked list contains a cycle
  • Solution: Floyd’s Cycle Detection Algorithm (Tortoise and Hare Method):
    • Two pointers: slow and fast
      • slow moves one step at a time
      • fast moves two steps at a time
    • If they meet, a cycle exists
  • Steps to find the cycle start:
    • After detecting a cycle, reset one pointer to the head
    • Move both pointers one step at a time until they meet
    • The meeting point is the start of the cycle
  • Mathematical reasoning for the algorithm:
    • The distance traveled by the pointers can be represented as:
      • x+y+n×z=2×(x+y), where n is the number of cycles

Performance Analysis

  • Array vs. Linked List:
    • Array:
      • Access: O(1)(random access via index)
      • Insert/Delete: O(n) (shifting elements)
    • Linked List:
      • Access: O(n) (sequential traversal)
      • Insert/Delete: O(1) (if the reference to the node is provided)
  • Applications:
    • Array: Suitable for frequent lookups and indexed operations
    • Linked List: Suitable for dynamic insertions/deletions and sparse data