2. If position is less than 1, or larger than list size, throw ListException 3. If the position is 1, change the address in head to the 2nd node 4. If the position is greater than 1 a. Point Node variable previous to 1st node b. Point Node variable current to 2nd node
Point previous to current node ii. Point current to next node d. Point the previous node to node after current node (“skip” over current node, thus removing the node) 5. Decrease size of list by 1
linked list, method remove() must also loop through the nodes to remove a node • In worst case, remove() loops n-2 times to remove the last node in the linked list, so Big-O is O(n-2) = O(n)
LinkedList, which starts counting from zero (0) • However, my LinkedList class starts counting from one (1) • For me, counting from one (1) makes more sense, since people are using the linked list class • So if you use the Java API LinkedList class, be aware of the difference with my code for the LinkedList class
integer position to 1 3. Loop through all the nodes a. Add position and toString() method of node’s data to end of string csvFormat b. Increment position 4. Return string csvFormat
current != null; current = current.getNext()){ csvFormat list toString() Method 3 head size A data next data next B C data next null " " 1 position current
+ "\n"; • 1st loop: add to end of csvFormat csvFormat list toString() Method 3 head size A data next data next B C data next null 1 position current 1, A
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2
csvFormat = csvFormat + position + ", " + current.toString() + "\n”; csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 2
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3
csvFormat = csvFormat + position + ", " + current.toString() + "\n"; csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 3
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4 null
for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4 null
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture