# CSE131 Module 6: Solutions to Practice Problems

Be sure to ask if you are surprised by any of these answers.

1. In the explanations below, if a is a LinkedList, then |a| is its length.
• `foo(int n)` returns a new LinkedList representing the numbers between 0 and n (inclusive) in order. Note that the code starts from n and counts down to 0, but the items are prepended to the list so the correct order results.
• `bar(LinkedList numbers)` returns the sum of the square roots of the Integers in the list.
• `baz(LinkedList a, LinkedList b)` returns a new LinkedList representing the pairwise concatenation of the Strings from the two lists (separated by a space). If a is longer than b, the code only uses the first |b| elements of a, and vice versa for b. For example, if
```a =	candy, pine, shower, cse131, hmmm
b =	apple, tree, mat, rocks
```
then
```baz(a,b) = candy apple, pine tree, shower mat, cse131 rocks
```
2. ```LinkedList<Integer> pairwiseSum(LinkedList<Integer> list1, LinkedList<Integer> list2) {
Iterator<Integer> iter1 = list1.iterator();
Iterator<Integer> iter2 = list2.iterator();
while (iter1.hasNext() || iter2.hasNext()) {
int temp = 0;
if (iter1.hasNext())
temp += iter1.next();
if (iter2.hasNext())
temp += iter2.next();
}
return result;
}
```
3. ```LinkedList<Object> reverse(LinkedList<Object> list) {
Iterator<Object> listIter = list.iterator();
while (listIter.hasNext())
return result;
}
```

or...

```LinkedList<Object> reverse(LinkedList<Object> list) {
for (Object x : list)
return result;
}
```
4. ```LinkedList<Double> multiplyPosition(LinkedList<Double> list) {
Iterator<Double> listIter = list.iterator();
while (listIter.hasNext()) {
}
return result;
}
```

or...

```LinkedList<Double> multiplyPosition(LinkedList<Double> list) {