Linked List Operations

DSA · Linked Lists

Theory

The core operations are inserting at the head, tail or after a node, deleting a node by value, searching, and reversing. Reversal walks the list once while redirecting each node's next pointer to the previous node, using three variables: prev, cur and next. Careful pointer updates in the right order are the whole skill: updating a pointer too early loses the rest of the list.

Why we use it

These operations appear in almost every linked-list interview question and teach careful pointer manipulation.

When to use it

Use them when implementing stacks, queues, playlists, or when solving reversal, merge and removal problems.

How it works

  1. Choose operation
  2. Insert at head? Yes: new.next = head; head = new. No: Find the node before the position.
  3. Delete? Yes: prev.next = prev.next.next. No: new.next = prev.next; prev.next = new.
  4. Done: only pointers change, no shifting

Syntax

Reverse:
prev = None; cur = head
while cur:
    nxt = cur.next
    cur.next = prev
    prev, cur = cur, nxt
head = prev        # O(n) time, O(1) space

Example

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

def build(values):
    head = None
    for v in reversed(values):
        head = Node(v, head)
    return head

def show(head):
    out = []
    while head:
        out.append(str(head.val)); head = head.next
    return " -> ".join(out)

def delete_value(head, target):
    dummy = Node(0, head)
    cur = dummy
    while cur.next:
        if cur.next.val == target:
            cur.next = cur.next.next
        else:
            cur = cur.next
    return dummy.next

def reverse(head):
    prev = None
    while head:
        nxt = head.next
        head.next = prev
        prev, head = head, nxt
    return prev

h = build([1, 2, 3, 4, 5])
h = delete_value(h, 3)
print("After deleting 3:", show(h))
print("Reversed:", show(reverse(h)))

Key points

DSA topics

Introduction to DSA

Arrays and Searching

Sorting Algorithms

Linked Lists

Stacks and Queues

Hashing

Trees

Heaps

Graphs

Algorithm Techniques

Classic Algorithms

Dynamic Programming

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Data Structures & Algorithms

The complete DSA roadmap: theory, why and when to use every structure and algorithm, Big O complexity, and Python code you can run right here in the browser.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice DSA Online 251 problems across 13 patterns, with hidden tests and step-by-step optimal solutions. Start practising →
Section {{ sec.num }}

{{ sec.label }}

DSA  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ current.why }}

When to use it

{{ current.when }}

Flowchart — how {{ current.name }} works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Step-by-step visualisation
{{ cell.v }}
{{ cell.i }} {{ cell.tag }}
Step {{ vizStepLabel }}: {{ vizNote }}
■ comparing ■ swapping / moving ■ done / found ■ out of range
Complexity & Pseudocode
{{ current.syntax }}
Example Code
{{ current.example }}
▶ Try It Live — runs real Python 3 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic