LinkedList Flashcards
Is a Linked List Random or Sequential Access?
Sequential
Is a LinkedList a linear data structure?
Yes
Each element is created as a separate…
Object, referred to as a Node
A Node is made up of two parts. What are they?
- The data (could be complex objects)
- The reference (or pointer) the the next Node in the List.
What is a real-world analogy for a ACCESSING an element in a LinkedList?
A tape measure. To get to inch 72 you first have to traverse inches 1 through 71.
How do we know when we have reached the end of a LinkedList?
The reference to the next Node will be null.
(Or a sentinel Node)
What is the term for the first Node in a LinkedList?
The HEAD Node.
What is the term for the last Node in a LinkedList?
The TAIL Node.
What are the three places in a LinkedList that we can ADD or REMOVE data?
HEAD, MIDDLE, TAIL.
How do we ADD a Node to the Head of LinkedList?
1) Create a Node, from the given value
2) make the new head Node point to the old head Node
3) update what Node we consider to be the head Node.
How do we DELETE a Node from the Head of LinkedList?
We make the 2nd Node in the list our new head Node and make the old head Node point to null.
How do we ADD a Node somewhere in the middle of a LinkedList? (2 steps)
- Make the pointer of the new Node point to the Node after the location we want to insert at.
- Set the Node before the location we want to insert at to point towards the new Node.
How do we REMOVE a Node somewhere in the middle of a LinkedList?
1) Make the pointer of the Node previous to the one we’re removing, to point to the Node after the one we’re removing.
2) Make the removed node point to null
How do we REMOVE a Node at the tail of a LinkedList?
Set the previous tail to point to a null value instead of the current tail.
What is the BigO of ACCESSING an element in a LinkedList?
O(n). Sequential data structure, worst-case you have to go via every element in the list.