Linked Lists
A linked list is a linear data structure where each element is a separate object.
{val: 1, next:
{val: 2, next:
{val:3, next:null}
}
}
A Sequence of Singly-Linked List Items
Diagram Me!
Singly Linked List:
-- is a data structure which holds a sequence of nodes.
-- each node contains data, and a pointer to another node
nodes can be ANYWHERE in memory, diagram is for mental model only
Why linked lists?
- Efficient -- Easy to add new nodes without needing to change the size of an array.
Arrays are contiguous blocks of memory...
Scavenger Hunt
-- Each node can be thought of as a location in a scavenger hunt. Each location has some data, and points you to the next location.
Your Turn!
Write a function that takes in an array, and creates a singly linked list for any inputArr length
function createSimpleLinkedList (inputArr) {
// some code
}
createSimpleLinkedList([1,2,3])
=> { val: 1, next: { val: 2, next: { val: 3, next: {} } } }
Solution To Simple Singly Linked List
Write a function that takes in an array, and creates a singly linked list
function createSimpleLinkedList (inputArr) {
var list = {};
for (i = inputArr.length - 1 ; i >= 0 ; i--) {
list = {val:inputArr[i], next:list};
} // END FOR LOOP
return list;
} // END FUNCTION
Singly Linked List Using Constructors:
2 Components
1. A prototype for a singlyList
2. A prototype for a Node
Both are needed to construct a functional list.
Anatomy of a Singly Linked List
// create a new list
list = new SinglyLinkedList();
// { head: null, tail: null, length: 0 }
// push a new node to the list
list.push(5)
// {
// head: Node { val: 5, next: null },
// tail: Node { val: 5, next: null },
// length: 1
// }
create a new list, and add a single node
Take a Guess!
What is the space and runtime complexity of inserting into the tail of a linked list?
Take a Guess!
What is the space and runtime complexity of inserting into the tail of a linked list?
O(1)
Why is appending to a singly linked list O(1)? Because a variable, called the tail, is kept that points to the end of the list. Whenever you need to add or remove an item from the end, you simply have to update the tail.
Take a Guess!
What if we don't have a tail pointer? What is the time and space complexity now?
Anatomy of a Linked List
5 Min Break!
Doubly linked lists
A doubly linked list is a list that has two references, one to the next node and another to previous node
Singly Linked List Using Constructors:
Still 2 Components
!!
How a Node is Constructed in a Doubly Linked List
Adding a node to the doubly linked list
Pushing a new node onto the tails of a doubly linked list
Double Linked List in action
list = new DoublyLinkedList();
// { head: null, tail: null, length: 0 }
list.push(5);
// {
// head: Node { val: 5, next: null, prev: null },
// tail: Node { val: 5, next: null, prev: null },
// length: 1
// }
list.push(8);
// {
// head:
// Node {
// val: 5,
// next: Node { val: 8, next: null, prev: [Circular] },
// prev: null },
// tail:
// Node {
// val: 8,
// next: null,
// prev: Node { val: 5, next: [Circular], prev: null } },
// length: 2
// }
Linked List Facts:
-
The DOM is a linked List(graph)!
-
node.nextSibling, node.parentNode...
-
-
Linked Lists are NOT indexed
-
Linked Lists DON'T support arbitrary access
-
Adding removing from a Linked List is often faster!
-
Wiki on tradoffs for linked lists
Go forth and link some lists!
Linked Lists
By Matthew Williams
Linked Lists
A walkthrough of linked lists
- 831