Python Tutorial

Linked Lists

A chain of nodes where each node points to the next — flexible insertion without shifting elements.

Nodes and Links

Unlike an array, a linked list stores elements in separate nodes scattered in memory. Each node holds a value and a reference to the next node. The list knows only its head; you follow links to reach the rest.

This makes inserting or deleting at a known position O(1) (just rewire pointers) but accessing the k-th element O(n) (you must walk the chain).

Arrays vs Linked Lists

OperationArray / ListLinked List
Access by indexO(1)O(n)
Insert/delete at frontO(n)O(1)
Insert/delete at known nodeO(n)O(1)
MemoryCompactExtra pointer per node

A Singly Linked List

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push_front(self, value):        # O(1)
        node = Node(value)
        node.next = self.head
        self.head = node

    def append(self, value):            # O(n)
        node = Node(value)
        if not self.head:
            self.head = node
            return
        cur = self.head
        while cur.next:
            cur = cur.next
        cur.next = node

    def __iter__(self):
        cur = self.head
        while cur:
            yield cur.value
            cur = cur.next

ll = LinkedList()
ll.append(1); ll.append(2); ll.push_front(0)
print(list(ll))     # [0, 1, 2]

Searching and Deleting

    def delete(self, value):            # O(n)
        cur = self.head
        prev = None
        while cur:
            if cur.value == value:
                if prev:
                    prev.next = cur.next   # unlink
                else:
                    self.head = cur.next   # delete head
                return True
            prev, cur = cur, cur.next
        return False

Deletion is O(1) once you hold the node's predecessor — the cost is finding it.

Reversing a Linked List

Reversing in place is a classic interview problem — flip each next pointer while walking once through the list.

def reverse(head):
    prev = None
    cur = head
    while cur:
        nxt = cur.next    # remember the rest
        cur.next = prev   # flip the link
        prev = cur        # advance
        cur = nxt
    return prev           # new head

Variants and the Cycle Trick

  • Doubly linked list: each node also points to the previous node — O(1) deletion given only the node.
  • Circular linked list: the tail points back to the head.

Floyd's fast/slow pointers detect a cycle in O(n) time, O(1) space:

def has_cycle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow is fast:
            return True
    return False

Best Practices

  • Choose a linked list when you insert/delete often and rarely index randomly.
  • For most Python code, a list or deque is faster due to cache locality.
  • Guard against losing references — save next before rewiring.
  • Use deque instead of hand-rolling a doubly linked list in practice.

Try It Yourself

Exercise 1: Count the nodes in a singly linked list.

Show solution
def length(head):
    n = 0
    cur = head
    while cur:
        n += 1
        cur = cur.next
    return n

Exercise 2: Why is accessing the k-th element O(n) in a linked list but O(1) in an array?

Show solution

An array computes the element's address directly; a linked list must follow k pointers from the head.

📘 Real-World Deep Dive

Knowing <strong>DSA Linked Lists (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Linked Lists that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

class Node:
    __slots__ = ("val", "next")
    def __init__(self, val, nxt=None):
        self.val = val; self.next = nxt
    def __repr__(self):
        return f"{self.val}→{repr(self.next) if self.next else '∅'}"

def from_list(xs):
    head = None
    for x in reversed(xs):
        head = Node(x, head)
    return head

print(from_list([1, 2, 3]))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Linked Lists as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Linked Lists

Common questions about this page.

What is Linked Lists?

Linked Lists is a DSA lesson that explains linked lists in Python. A chain of nodes where each node points to the next — flexible insertion without shifting elements. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run linked lists examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn linked lists in this DSA Python lesson (Linked Lists).

How do I use linked lists in Python?

To use linked lists in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of linked lists?

This Linked Lists tutorial shows linked lists syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Linked Lists example for beginners

Yes. This page includes a beginner linked lists example you can copy and run. It is designed for searches such as "linked lists for beginners", "linked lists example", and "how to use linked lists".

What are common mistakes with linked lists?

Common linked lists mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn linked lists?

Linked Lists is used in real Python work. Learning linked lists helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Linked Lists free to learn online?

Yes. You can learn linked lists free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.