4.6 Exercises¶
4.6.1 Check your understanding¶
1. Finding elements in arrays and linked lists¶
An array and a singly linked list both store [A, B, C, D, E] in order. We want to read the fourth element, D:
- Which index can we use to access it directly in the array?
- Starting at the head node
A, which nodes must the singly linked list visit by followingnext? - As the requested element moves farther toward the end, how does the number of steps change for each structure? Which is better suited to repeated access by position, and why?
Reference answer
-
With zero-based indexing, the fourth element has index 3, so the array can read
arr[3]directly. -
The singly linked list must start at the head and follow the path
A → B → C → D, advancing alongnextthree times. -
An array can locate an element directly from its starting address and the index, giving time complexity \(O(1)\) for access by position. To access the \(k\)-th node of a singly linked list, we must start at the head and follow
next\(k-1\) times, taking \(O(n)\) time in the worst case.This comparison concerns access by position only; it does not mean that linked lists are slower for every operation.
2. Inserting elements into arrays and linked lists¶
An array and a singly linked list both store A, B, C, D. We 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 we already have a reference to nodeB.
- Which elements must the array move? Write out the array after insertion.
- In what order should the linked list update
X.nextandB.next? Write out the resulting list. - Why must a comparison of insertion efficiency specify that we already have a reference to node B?
Reference answer
-
First move
Done position to the right, then moveCone position to the right, and finally putXat index 2. The result is[A, B, X, C, D]. -
Initially,
B.nextpoints toC. First setX.next = B.next, makingXpoint toC. Then setB.next = X. The result isA → B → X → C → D. If we overwriteB.nextfirst without saving the original link, we may lose access toC. -
Once the location of
Bis known, insertion into the linked list requires changing only two links and takes \(O(1)\) time. If we first need to search forBfrom the head, that search itself may take \(O(n)\) time.
3. How does a list's capacity grow?¶
An array-based list currently contains [A, B, C], with length size = 3 and capacity capacity = 4.
When capacity runs out, the new array is allocated with twice the previous capacity.
- After appending
D, what are the list's length and capacity? Does it need to grow? - When
Eis appended next, what does the capacity become? How many existing elements must be copied? - If the length of the underlying array cannot change, why does the list's capacity appear to grow?
Reference answer
-
Dfits in the last empty position. The contents become[A, B, C, D], withsize = 4andcapacity = 4. No capacity increase is needed. -
Appending
Efinds no empty position, so a new array of capacity 8 is created. The four existing elementsA, B, C, Dare copied into it, and thenEis added. The list now hassize = 5andcapacity = 8. -
The original array itself does not become longer. The list creates a larger array, copies the existing elements, and switches to using the new array for storage. From the user's perspective, its capacity has grown.
4.6.2 Programming exercises¶
1. Adding one to a large integer stored in 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]; all other inputs have a nonzero first digit.
Simulate the usual digit-by-digit method for decimal addition to add 1 to this integer, and return the result in the same array format.
You may modify digits directly. If a carry creates a new leading digit, you may return a longer array.
Hints
- Start at the last digit of the array, 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 all digits are 9, add a leading 1.
2. Reversing a singly linked list¶
You are given the head node head of a singly linked list. Each node contains a value and a next reference to the following node.
Use iteration to reverse every link between nodes, and return the new head. Do not create any new linked-list nodes.
Hints
- First draw three connected nodes and the two pointers prev and cur on paper.
- Before changing 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.