

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
LinkedList has a field that refers to the last node in the list. Java's LinkedList remove(int index) method runtime is O(1). How is it possible even if your ...
Typology: Exercises
1 / 25
This page cannot be seen from the preview
Don't miss anything!
public E remove(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException();
first remove here (index = 3) null 2 obj
Node nodeBefore = first; for (int i = 0; ; i++) { nodeBefore = nodeBefore.next; } // remove the node … size--; return obj; }
first remove here (index = 3) current
Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node E obj = nodeBefore.next.data; nodeBefore.next = nodeBefore.next.next; size--; return obj; }
Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node Node nodeToRemove = nodeBefore.next; nodeBefore.next = nodeToRemove.next; size--; return nodeToRemove.data; }
private class Node { private E data; private Node prev; private Node next; private Node(E obj, Node previous, Node next){ data = obj; prev = previous; this.next = next; }
null last null first
How would you implement addFirst? null last null first How would you implement addLast?