4.6 Exercises¶
4.6.1 Check your understanding¶
1. How do arrays and linked lists locate elements?¶
An array and a singly linked list each store [A, B, C, D, E] in that order. You want to read the fourth element, D.
- Which index can you use to read the element directly from the array?
- Starting at the head node
A, which nodes do you visit in order as you follownextin the singly linked list? - How does the number of steps change in each structure as the desired position moves farther from the start? Which structure is better suited to repeated reads by position, and why?
Sample answer
-
With indexing from 0, the fourth element has index 3, so you can read
arr[3]directly. -
In the singly linked list, you must start at the head. The path is
A → B → C → D, which requires followingnextthree times. -
An array can locate an element directly from the base address and index, so access by position takes \(O(1)\) time. To reach the \(k\)-th node in a singly linked list, start at the head and follow
nexta total of \(k-1\) times. In the worst case this takes \(O(n)\) time.This comparison applies only to reading by position. A linked list need not be slower for every operation.
2. How do arrays and linked lists insert elements?¶
An array and a singly linked list each contain A, B, C, D. You want to insert X after B.
- The array has capacity 5 and currently contains
[A, B, C, D, _]. - The linked list is
A → B → C → D, and you already have a reference to nodeB.
- Which array elements must move? Write out the array after insertion.
- In what order should you update
X.nextandB.next? Write out the linked list after insertion. - Why does it matter for the efficiency comparison that you already have a reference to node B?
Sample answer
-
First move
Done place to the right, then moveCone place to the right, and finally putXat index 2. The resulting array is[A, B, X, C, D]. -
Initially,
B.nextpoints toC. First setX.next = B.nextso thatXpoints toC. Then setB.next = X. The result isA → B → X → C → D. If you overwriteB.nextfirst without saving the original link, you may lose track ofC. -
Once you have located
B, insertion in the linked list changes just two links and takes \(O(1)\) time. If you have to findBby searching from the head, the search itself can take \(O(n)\) time.
3. How does list capacity grow?¶
An array-backed list contains [A, B, C], with size = 3 and capacity = 4.
When there is no room for another element, the new array has twice the capacity of the old one.
- After you append
D, what are the length and capacity of the list? Does the capacity need to grow? - When you then append
E, what is the new capacity? How many existing elements must you copy? - The underlying array has a fixed length. Why, then, does the capacity of the list appear to grow?
Sample answer
-
Dfits in the last available slot. The list now contains[A, B, C, D], withsize = 4andcapacity = 4. No increase in capacity is needed. -
When you append
Enext, there are no slots left. Create a new array with capacity 8, copy all four existing elements (A, B, C, D) into it, and then appendE. The list now hassize = 5andcapacity = 8. -
The original array keeps its length. The list creates a larger array, copies the existing elements into it, and uses it as the new underlying storage. From the user's perspective, the capacity has grown.
4.6.2 Programming exercises¶
1. Add one to a large integer represented by an array¶
The array digits stores the digits of a nonnegative integer from left to right. For example, [3, 0, 8] represents 308.
Zero is represented by [0]. Every other input starts with a nonzero digit.
Add 1 to the integer as you would by hand in base ten. Return the result in the same array format.
You may modify digits directly. If there is a carry past the first digit, you may return a longer array.
Hints
- Start at the last digit, just as you would when adding by hand.
- If the current digit is less than 9, increment it and return immediately.
- If the current digit is 9, change it to 0. If every digit is 9, insert a 1 at the front.
2. Reverse a singly linked list¶
You are given the head node head of a singly linked list. Each node has a value and a next reference to the next node.
Use iteration to reverse all the links between nodes and return the new head. Do not create any new list nodes.
Hints
- On paper, draw three linked nodes and two pointers, prev and cur.
- Before overwriting cur.next, save the original next node in nxt.
- After reversing cur.next, set prev = cur and then cur = nxt to continue with the next node in the original list.