Data Structures19 sections · 729 units
Open in Course

Node Structure

The building block

A singly linked list node contains two fields: pseudocode class ListNode: val: integer next: ListNode or null The val field stores the data.

The next field points to the following node, or null if you've reached the last node. To traverse a list, start at the head and follow next pointers until you hit null.

The operation takes O(n)O(n) time where nn is the list length. Memory layout differs from arrays. Array elements sit in contiguous memory, enabling cache-friendly access.

Linked list nodes scatter across memory, causing more cache misses. That's why arrays are faster for sequential access despite both being O(n)O(n). Time: O(n)O(n). Space: O(1)O(1).