Java Reference
In-Depth Information
18. The two ways to change an object of our linked list class are to change its front
reference or to change the next or data field of an existing node.
19. Inserting and removing is most expensive at the end of the list, because the code
must loop through all of the next references to reach the end. This situation is the
opposite of the array list, which inserts/removes most slowly at the start because of
the shifting of elements that is required.
20. The loop should stop at index i - 1 , the index before the one to add or remove.
This should occur so that you can adjust the preceding node's next reference.
21. Resizing is not necessary for a linked list, since more nodes can be dynamically
allocated. The only limit on the number of elements is the amount of memory
available to the Java virtual machine.
22. ListNode current = list;
while (current != null) {
current.data = 42;
current = current.next;
}
23. ListNode current = list;
while (current.next != null) {
current = current.next;
}
current.data = 42;
24. ListNode current = list;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(42);
25. public int min() {
if (front == null) {
throw new IllegalStateException();
} else {
int minValue = front.data;
ListNode current = front.next;
while (current != null) {
minValue = Math.min(minValue, current.data);
current = current.next;
}
return minValue;
}
}
Search WWH ::




Custom Search