600.226 – Homework 6 Solution - Spring 2013 Part B) Short Answer [30 pts] [8] Write a non-recursive algorithm, using pseudocode only, to print a singly linked list (with a head reference, but no tail pointer) in reverse, using only constant extra space (ie, no copying to an array). Assume the method is a member of a singly linked list class with nested Node class (much like our SLList implementation). What is the running time of your algorithm? printReverse(Node head): // 6 pts size=1 Node n = head;   while ( n is not the last node ) {     advance n to next node size++; }   for ( i=size-1; i>=0; i--) {      n = head;    for ( j=0; j l = new LinkedList< String >(); l.addFirst("aaa"); l.addLast("bbb"); l.addFirst("ccc"); l.addFirst("ddd"); /* DRAW A */ ddd <--> ccc <--> aaa <--> bbb // 2 pts ListIterator iter = l.listIterator(); iter.next(); iter.add("eee"); iter.add("fff"); iter.next(); /* DRAW B */ ddd <--> eee <--> fff <--> ccc <--> aaa <--> bbb // 2 pts ^ iter iter.next(); iter.remove(); iter.previous(); iter.add("ggg"); /* DRAW C */ ddd <--> eee <--> fff <--> ggg <--> ccc <--> bbb // 2 pts ^ iter iter.previous(); iter.remove(); iter.next(); iter.add("hhh"); /* DRAW D */ ddd <--> eee <--> fff <--> ccc <--> hhh <--> bbb // 2 pts ^ iter This question refers to the Java API interfaces and classes. Suppose you have a List L of items and a SortedSet S of positive integers in descending order. [5] Using only methods available through those (List and SortedSet) API interfaces (including their super-interfaces), write the body of this generic static method printSelect(L,S) that prints only the items in L that are in positions corresponding to the index values in S. Try to be as efficient as possible. public static void printSelect(List L, SortedSet S) { T temp; for (Integer idx : S) { temp = L.get(idx); System.out.println(temp); } } [1] What is the running time of your method if L and S are implemented with Java ArrayLists? O(n) n = S.size(); [1] What is the running time if L and S are implemented with Java LinkedLists? O(nm) n = S.size(); m = L.size(); [1] Can either be improved with a different method implementation? Yes, the second one can be O(n+m) if you first create the reverse of S (using O(n) extra space), and then walk along L with an iterator as you go through S.