- 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.

- What changes do you need to make to a linked list in order to have a
*constant time access*to the last node?The linked list should have a tail pointer in addition to a head pointer.

- 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) - 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) - 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 - 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 listAn 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".

- 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;`

- 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

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.Node toInsert = new Node(6);

nextNode = prevNode.getNext(); //now nextNode is 5 prevNode.setNext(toInsert); //prevNode is 9 nextNode.setPrev(toInsert); toInsert.setNext(nextNode); toInsert.setPrev(prevNode);

- 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; if(head == null) return; while(head != null && head.data.equals(keyItem)){ head = head.next; } Node temp = head; Node prev = head; while(temp != null){ if(temp.data.equals(keyItem)){ prev.next = temp.next; }else{ prev = temp; } temp = temp.next; } }

- 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); if(head == null){ head = newNode; return; } Node temp = head; Node prev = head; while(temp != null && temp.data.compareTo(keyItem)>0){ prev = temp; temp = temp.next; } prev.next = newNode; newNode.next = temp; }