## Self-Review Questions

1. Given an array and a singly linked list. Which of these data structures uses more memory space to store the same number of elements? Explain your answer.

A singly linked list, because it also needs to maintain the pointers to the other nodes, which takes up space.

2. What changes do you need to make to a linked list in order to have a constant time access to the last node?

3. What is the worst-case complexity of searching in a linked list with n nodes?

a)   O(1)
b)   O(log n)
c)   O(n)
d)   O (n^2)

4. What is the worst-case complexity of merging two linked lists with n nodes?

a)   O(1)
b)   O(log n)
c)   O(n)
d)   O (n^2)

5. How many references must you change to delete a node from the middle of a singly linked list?

a)   1
b)   2
c)   3
d)   0

6. Why one might choose to use a singly linked list instead of a doubly linked list?

a)   Insert is not efficient for a doubly linked list
b)   A doubly linked list has a fixed size
c)   Memory usage is a big concern for you
d)   Remove takes constant time for a singly linked list

An alternative to a standard deletion strategy is known as lazy deletion. When deleting elements from a singly linked list, we delete them logically but not physically. This is done by marking the node as deleted (using a boolean value). The numbers of deleted and not-deleted elements in the list are kept as part of the list. If at some point the number of deleted elements is equal to the number of not-deleted elements, we traverse the list and delete all "lazily deleted" elements. Discuss the advantages and disadvantages of this scheme.

Advantages: It takes less time to mark a node as deleted than to change pointers.

Disadvantages: Once a node has been "lazily deleted" it still needs to be traversed in a search for another node. Also, you then use up a lump amount of time to delete all the "lazily deleted elements".

7. Given a doubly linked list where each node has two references (prev and next): one that points to a previous node and another to a next node: Assuming the linked list above, provide the output for the following code fragments. The list is restored to its initial state before each line executes:

a)   `____5_____`     `head.next.next.next.data;`

b)   `____3_____`     `head.next.next.prev.prev.data;`

c)   `____9_____`     `tail.prev.prev.prev.next.data;`

8. Given a doubly linked list where each node has two references (prev and next): one that points to a previous node and another to a next node. Write the statements to insert a new node

`Node toInsert = new Node(6);`
containing the 6 between the node with the 5 and the node with the 9. You do not need to write the whole method but just the statements to make the connections.

```nextNode = prevNode.getNext(); //now nextNode is 5
prevNode.setNext(toInsert); //prevNode is 9
nextNode.setPrev(toInsert);
toInsert.setNext(nextNode);
toInsert.setPrev(prevNode);
```
9. Implement a Java method
`public void removeAllMatchingItems(AnyType keyItem)`

that removes each and every item equal the keyItem from a singly-linked list. The list is not changed in any other way - if the requested item is not contained within the list, the method leaves the list in its prior condition You assume the LinkedList class given in lectures.

```public void removeAllMatchingItems(AnyType keyItem){

if(list == null)
return;
return;

}

while(temp != null){
if(temp.data.equals(keyItem)){
prev.next = temp.next;
}else{
prev = temp;
}
temp = temp.next;
}
}```
10. Given a sorted singly-linked list, where the head contains the smallest element Implement a Java method
`public void insertInOrder(Comparable keyItem)`

that creates a new node and inserts it in-order into the list. You assume the LinkedList class given in lectures.

```public void insertInOrder(Comparable keyItem){
Node newNode = new Node(keyItem);

return;
}