Problem overview
The method moveToFront receives a to-do list items (a list of strings) and a number index. It returns the list with the item at index moved to the front. The other items keep their order. The list has one to ten words, index is always a valid position, and the same word may appear more than once.
For ["wash", "shop", "cook", "read"] and index = 2 the item at index 2 is "cook". It moves to the front, and "wash" and "shop" shift one place right: ["cook", "wash", "shop", "read"]. With ["tea", "coffee"] and index = 0 nothing changes.
Approach
Moving an item is two edits: take it out of its old place, then put it in at index 0. remove(index) does the first and hands back the item it removed, and add(0, item) does the second, shifting everything else right. The order matters: the item must be out of the list before it goes back in, or it would be in the list twice.
When index is 0, the item is removed from the front and added to the front again, so the list ends up unchanged without a special case.
Step by step
- Remove by position with
items.remove(index)and keep the returned word in aStringnameditem. The items after it move one place left. - Insert at the front with
items.add(0, item). With an index first,addinserts instead of appending, and shifts the others right. - Return
items. The method changes the list it was given and returns that same list.
The solution in Java
class Solution {
public List<String> moveToFront(List<String> items, int index) {
String item = items.remove(index);
items.add(0, item);
return items;
}
}
Details worth knowing:
Listhas tworemovemethods:remove(int index)removes by position and returns the removed item, whileremove(Object o)removes the first item equal tooand returns aboolean. Hereindexis anint, so Java picks the first one, and the result fits in aString.- The method changes
itemsitself rather than building a new list. Returning it is what hands the result back.
Edge cases and common mistakes
- Removing by value. Writing
items.remove(items.get(index))removes the first matching word, not the one atindex. For["call", "pay", "call", "walk"]andindex = 2it producescall pay call walk, not the expectedcall call pay walk. Duplicates are exactly why the lesson removes by position. - Adding before removing. If you insert first, every old item from the front moves right, so the original is now at
index + 1. Callingremove(index)afterwards removes the wrong item: on the first example it givescook wash cook read, and"shop"is lost. - Index 0. This works without special handling; the list comes back unchanged.
- Forgetting to put the item back. The removed word is gone for good unless you add it again.
Tracing the first example
Start with ["wash", "shop", "cook", "read"] and index = 2. The call remove(2) takes out "cook" and leaves ["wash", "shop", "read"]. Then add(0, "cook") inserts it in front, which moves the three others one place to the right and gives ["cook", "wash", "shop", "read"]. The words that were in front of the moved item are the only ones whose indexes change, and they all go up by one.
Testing
Inputs and the output of the solution:
["wash", "shop", "cook", "read"],index = 2givescook wash shop read.["tea", "coffee"],index = 0givestea coffee.["call", "pay", "call", "walk"],index = 2givescall call pay walk.
