{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework4

# homework4 - 15-121 FALL 2009[CORTINA/REID-MILLER HOMEWORK 4...

This preview shows pages 1–6. Sign up to view the full content.

15-121 FALL 2009 [CORTINA/REID-MILLER] HOMEWORK 4 - due Saturday, October 3 by 11:59PM PROBLEMS (10 points) 1.(1.5 pts) (Revised version) Write a method that reverses the order of elements in an ArrayList<E> without creating a new ArrayList . You may only use the methods remove , add , and size . Explain why this method is inefficient. 2.(1.5 pt) The following method is supposed to remove all duplicates from an ArrayList of Integer : public static void removeDuplicates(ArrayList<Integer> list) { int numValues = list.size(); for (int i = 0; i < numValues-1; i++) { for (int j = i+1; j < numValues; j++) { if (list.get(i) == list.get(j)) list.remove(j); } } } 3.Clearly identify all logical errors in this implementation.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4.Derive the worst-case runtime complexity of this algorithm in Big-Oh notation if the list has n integers in it. When does this worst-case occur? 5.(2 pts) Assume that a public Node class is defined as follows: public class Node { public int data; public Node next; public Node(int data, Node next) { this.data = data; this.next = next; } } 6.Two singly linked lists of integers list1 and list2 are shown below _ -------- | | | | | list1 |----->| 1 |null| |_| | | | -------- _ ------- ------- -------- | | | | | | | | | | | list2 |----->| 2 | ---->| 3 | ---->| 4 |null| |_| | | | | | | | | | ------- ------- -------- To what singly linked lists do list1 and list2 refer after executing the following Java code? list1.next = list2; list2 = list2.next; list1.next.next = null; list2.next.next = list1;
list1 = list2.next; list2.next = null; For your answer you may use the format below. For example, you can write list2 above as

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
list2 --> [2] --> [3] --> [4] 7.Write Java code to convert the singly linked lists in the BEFORE picture to the AFTER picture by changing the links between the nodes. You should not create any new Node objects or change the values of nodes' data fields. You may, however, create a single Node variable that refers to an existing node if you need it. BEFORE: _ ------- -------- | | | | | | | | list1 |----->| 2 | ---->| 4 |null| |_| | | | | | | ------- -------- _ ------- -------- | | | | | | | | list2 |----->| 1 | ---->| 3 |null| |_| | | | | | | ------- -------- AFTER: _ ------- ------- ------- -------- | | | | | | | | | | | | | | list1 |----->| 1 | ---->| 2 | ---->| 3 | ---->| 4 |null| |_| | | | | | | | | | | | | ------- ------- ------- -------- ---- | | list2 |null| | | ---- 8.(1.5 pts) A singly linked list of integers is shown below.
_ ------- ------- ------- -------- | | | | | | | | | | | | | | head |----->| 4 | ---->| 7 | ---->| 2 | ---->| 6 |null| |_| | | | | | | | | | | | | ------- ------- ------- -------- 9.What does the following code fragment output for the list above? What does it output in general?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

homework4 - 15-121 FALL 2009[CORTINA/REID-MILLER HOMEWORK 4...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online