Class Notes Set 4: Computer Science - Data Structures: Linked Lists
Class: Computer Science 201
Date: November 8th
Topic: Data Structures: Linked Lists
Note Style: Detailed Explanation with Code Snippets (Pseudo-code)
I. Introduction to Linked Lists
* Definition: A linear data structure where elements (nodes) are not stored at contiguous
memory locations. Instead, each node contains a data field and a reference (or pointer) to the
next node in the sequence.
* Contrast with Arrays:
* Arrays: Fixed size, contiguous memory, direct access by index (O(1)).
* Linked Lists: Dynamic size, non-contiguous, sequential access (O(N)).
* Use Cases:
* Implementing Stacks/Queues
* Polynomial representation
* Dynamic memory allocation (OS)
* Any situation needing efficient insertions/deletions at arbitrary positions.
II. Components of a Linked List
A. Node: The fundamental building block.
* data: The actual value stored.
* next: A pointer/reference to the subsequent node. (Last node's next is typically NULL or
None).
B. Head: A pointer to the first node in the list. If head is NULL, the list is empty.
C. Tail (optional but common): A pointer to the last node. Speeds up appending.
III. Types of Linked Lists
A. Singly Linked List (SLL):
* Each node points only to the next node.
* Traversal is unidirectional (forward only).
B. Doubly Linked List (DLL):
* Each node has a next pointer and a prev (previous) pointer.
* Allows bidirectional traversal.
* More memory per node, but faster backward traversal and some operations.
C. Circular Linked List (CLL):
* The next pointer of the last node points back to the head node.
* Useful for round-robin scheduling, continuous loops.
IV. Common Operations & Time Complexity
Operation Singly Linked List (SLL) Doubly Linked List (DLL) Array (Comparison)
Notes
SearchO(N) O(N) O(N) Must traverse linearly
Insertion
- At Head O(1) O(1) O(N) Just update head pointer
- At Tail O(N) (without tail ptr) O(1) (with tail ptr) O(1) (amortized) SLL needs
traversal to find end
- At Mid (k-th) O(N) O(N) O(N) Need to traverse to k-1th node
Deletion
- At Head O(1) O(1) O(N) Update head pointer
- At Mid (k-th) O(N) O(N) O(N) Need to traverse to k-1th node. DLL easier to fix prev
pointers.
V. Pseudo-code Example: Singly Linked List Insertion at Head
Generated code
class Node:
data
next_node_pointer
class LinkedList:
head = NULL // Initialize empty list
function insertAtHead(value):
newNode = new Node()
newNode.data = value
newNode.next_node_pointer = head // New node points to old head
head = newNode // Head now points to new node
Use code with caution.
VI. Disadvantages of Linked Lists
* More memory: Each node stores data + a pointer.
* No random access: Cannot jump directly to the i-th element. Requires traversal.
* Cache performance: Data is not contiguous, potentially worse cache utilization.
VII. When to Choose Linked Lists:
* Frequent insertions/deletions, especially at the beginning or middle.
* When the size of the list is highly dynamic and unknown beforehand.
* When memory usage per element (pointer overhead) is acceptable.