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

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [CORTINA/REID-MILLER]HOMEWORK 4 - due Saturday, October 3 by 11:59PMPROBLEMS (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 ArrayListof 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. 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 list1and list2are shown below _ --------| | | | |list1 |----->| 1 |null||_| | | |--------_ ------- ------- --------| | | | | | | | | | |list2 |----->| 2 | ---->| 3 | ---->| 4 |null||_| | | | | | | | | |------- ------- --------To what singly linked lists do list1and list2refer 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 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||_| | | |...
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 13

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online