Authored by Tony Feng
Created on March 20th, 2022
Last Modified on March 21st, 2022
Understanding Linked Lists
Main Concepts
- Linked lists are an ordered collection of objects. While other lists use a contiguous memory block to store references to their data, linked lists store references as part of their elements.
- Node = Data + Next
- A value
- A reference to the next node
- The first node of a linked list is called Head.
- The last node must have its Next pointing to None.
Different Types
- Singly Linked Lists (All linked lists mentioned in this file are this type.)
- Doubly Linked Lists
- Circular Linked Lists
Operations
- Access - O(N)
- Search - O(N)
- Insert - O(1)
- Commonly, a function of
insertion
containstraverse
andadd
, which is O(N).
- Commonly, a function of
- Delete - O(1)
Applications
- Queues - FIFO
- Stacks - LIFO
- Graphs
Linked List V.S. Array
Linked List | Arrays | |
---|---|---|
Pros | 1. Dynamic Size 2. Ease of insertion/deletion |
1. Fast access to elements 2. Linear data of similar types could be stored |
Cons | 1. More memory needed 2. Random access is not allowed 3. Not cache friendly |
1. Fixed size 2. Slow insertion/deletion 3. Waste of memory |
Linked List Implementation in Python
Using collections.deque
in Python
|
|
Creating a linked list from scratch in Python
|
|
Linked List Implementation in Java
|
|
Reference
- Linked Lists in Python: An Introduction
- 手把手带你刷Leetcode力扣 - 链表Linked List
- 手把手带你刷Leetcode力扣 - Python3链表常用操作
- 手把手带你刷Leetcode力扣 - Java链表常用操作