Exercise · Volume 1 · 2.2.4-2
Exercise 2.2.4-2
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K8, compute by machine: find a number or an object by a large computation from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
Draw the before and after pictures of the concatenation of two circular lists by operation (3), when and are not .
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Operation (3) swaps with , and then sets and .
Solution
Show the solution
Let the first list have nodes and the second have nodes . The pointer points at , whose link points at , and points at , whose link points at .
Before:
PTR1 = a_k PTR2 = b_l
a_1 -> a_2 -> ... -> a_k -> a_1
b_1 -> b_2 -> ... -> b_l -> b_1
After the swap of the two links, points at and points at . After and the picture is:
PTR1 = b_l PTR2 = null
a_1 -> ... -> a_k -> b_1 -> ... -> b_l -> a_1
It is one circle with nodes. The nodes of the first list come first, then the nodes of the second. The pointer is at , which is the rightmost node of the combined list, so the convention that the pointer is at the right end is kept.
Check
For and the walk from starts at , passes and returns to , which is five nodes, and is . A run in python over all confirmed the cycle of length .
Remarks
The operation takes constant time, whatever the lengths of the lists. This is one of the main reasons for using circular lists.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.