NeetCode
PracticeCoursesRoadmap
Sign InPro
PracticeCoursesRoadmap
Sign InPro
CoursesAlgorithms & Data Structures for Beginners

Progress

6/35

About

  • 0IntroductionFREE1m

Arrays

  • 1RAMFREE6m
  • 2Static Arrays15m
  • 3Dynamic Arrays16m
  • 4Stacks4m

Linked Lists

  • 5Singly Linked ListsFREE12m
  • 6Doubly Linked Lists10m
  • 7Queues4m

Recursion

  • 8Factorial11m
  • 9Fibonacci Sequence13m

Sorting

  • 10Insertion Sort19m
  • 11Merge Sort22m
  • 12Quick Sort17m
  • 13Bucket Sort14m

Binary Search

  • 14Search Array16m
  • 15Search Range8m

Trees

  • 16Binary Tree11m
  • 17Binary Search Tree15m
  • 18BST Insert and Remove22m
  • 19Depth-First Search15m
  • 20Breadth-First Search11m
  • 21BST Sets and Maps6m

Backtracking

  • 22Tree Maze14m

Heap / Priority Queue

  • 23Heap Properties14m
  • 24Push and Pop18m
  • 25Heapify15m

Hashing

  • 26Hash Usage10m
  • 27Hash Implementation29m

Graphs

  • 28Intro to Graphs22m
  • 29Matrix DFS22m
  • 30Matrix BFS14m
  • 31Adjacency List20m

Dynamic Programming

  • 321-Dimension DP20m
  • 332-Dimension DP22m

Bit Manipulation

  • 34Bit Operations17m

Linked Lists · Chapter 5 · 12 min

Singly Linked Lists

A singly linked list is a sequence of nodes where each node holds a value and a pointer to the next node. Unlike arrays, linked list nodes don't have to sit next to each other in memory.

Node Structure

Each node has two fields: a value and a next pointer. The list as a whole is tracked by a pointer to the first node (the head).

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

# 1 → 2 → 3 → None
head = ListNode(1, ListNode(2, ListNode(3)))

Traversing

To visit every element, walk the next pointers until you hit None:

curr = head
while curr is not None:
    print(curr.val)
    curr = curr.next

Why Not Just Use Arrays?

Linked lists shine when you're inserting or deleting at the front: both are O(1) because you just rewire a pointer. Arrays would have to shift every subsequent element.

| Operation | Linked List | Array | |---|---|---| | Access by index | O(n) | O(1) | | Insert at head | O(1) | O(n) | | Insert at tail | O(n) or O(1) with tail pointer | O(1) amortized | | Search | O(n) | O(n) |

Tip: If you find yourself pushing to the front of an array repeatedly, a linked list might be a better fit.

Previous

Stacks

Next

Doubly Linked Lists

On this page