Created
April 27, 2014 06:08
-
-
Save gabhi/11338700 to your computer and use it in GitHub Desktop.
Flattern linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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