Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 27, 2014 06:08
Show Gist options
  • Save gabhi/11338700 to your computer and use it in GitHub Desktop.
Save gabhi/11338700 to your computer and use it in GitHub Desktop.
Flattern linked list
import java.util.LinkedList;
import java.util.List;
public final class FlattenUtil {
public static List<Object> flatten(List<?> list) {
List<Object> retVal = new LinkedList<Object>();
flatten(list, retVal);
return retVal;
}
public static void flatten(List<?> fromTreeList, List<Object> toFlatList) {
for (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment