package algorithms; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class FlattenLinkedList { public static void main(String[] args) { // List of strings LinkedList stringList = new LinkedList(); for (Integer i = 0; i < 10; i++) { stringList.add("String #" +i.toString()); } // List of integers LinkedList intList = new LinkedList(); for (Integer i = 10; i < 20; i++) { intList.add(i); } // Nested Lists LinkedList nestedList = new LinkedList(); nestedList.add("Nested String 1"); nestedList.add("Nested String 2"); LinkedList bigList = new LinkedList(); bigList.add("First item"); bigList.add(stringList); bigList.add("Third Item"); bigList.add(intList); bigList.add("Fifth Item"); bigList.add(nestedList); bigList.add("Seventh Item"); List flattenedListRecursion = flattenList(bigList); List flattenedList = flattenListNoRecursion(bigList); printList(flattenedListRecursion); printList(flattenedList); } @SuppressWarnings("unchecked") /** * Flattens a given list. * @param list * @return */ public static List flattenList(List inList) { List newList = new LinkedList(); for (Object i : inList) { // If it's not a list, just add it to the return list. if (!(i instanceof List)) { newList.add(i); } else { // It's a list, so add each item to the return list. newList.addAll(flattenList((List)i)); } } return newList; } @SuppressWarnings("unchecked") public static List flattenListNoRecursion(List inList) { List tempList = null; // Clone the input list to newList List newList = new LinkedList(); newList.addAll(inList); ListIterator iterator = newList.listIterator(); int currentPosition = 0; while (iterator.hasNext()) { Object i = iterator.next(); if (!(i instanceof List)) { // If it's not a list, advance the position. Don't advance position if this IS a list. currentPosition++; } else { // If the current item is a list, save it to a temp var. tempList = (List) i; // Delete the list from the list iterator.remove(); // Add each item from the temp list to the master list at the same position the sublist was removed. for (Object obj : tempList) { iterator.add(obj); } // reset the iterator to re-walk the list that was just inserted (within the master) to check for more lists. iterator = newList.listIterator(currentPosition); } } return newList; } /** * Prints a given list. * @param list */ public static void printList(List list) { int i = 0; for (Object item : list) { System.out.println("List item #"+i +": "+item); i++; } } }