JAVA /** * deleteItemsInRange removes all items in a given range, it removes eve
ID: 3786066 • Letter: J
Question
JAVA
/**
* deleteItemsInRange removes all items in a given range, it removes every
* item that falls between lo and hi (inclusive), and you can assume lo <=
* hi.
*
* The boundaries of that range (lo and hi) need not be in the list, which
* means this method may leave the linked list unchanged.
*
* Examples: LinkedList : null, lo = 0, hi = 100 ==> Resulting list : null
* LinkedList : 4 --> 3 --> 7 --> null, lo = 0, hi = 100 ==> Resulting list
* : null LinkedList : 4 --> 3 --> 7 --> null, lo = 3, hi = 4. Resulting
* list : 7 --> null LinkedList : 4 --> 3 --> 7 --> null, lo = 1, hi = 2.
* Resulting list : 4 --> 3 --> 7 --> null
*/
function name:
public void deleteItemsInRange(int lo, int hi) {
// TODO
Explanation / Answer
Solution to create, initialize, search and delete nodes in range using java is as below:
import java.util.Iterator;
import java.util.LinkedList;
public class MainClass {
public static void main(String[] args) {
LinkedList<Integer> initialList = new LinkedList<Integer>();
initialList.add(4);
initialList.add(3);
initialList.add(7);
LinkedListDemo llDemo = new LinkedListDemo(initialList);
llDemo.deleteItemsInRange(3, 4);
llDemo.printList();
}
}
class LinkedListDemo {
private LinkedList<Integer> linkedList;
public LinkedListDemo(LinkedList<Integer> linkedList) {
super();
this.linkedList = linkedList;
}
public LinkedList<Integer> getLinkedList() {
return linkedList;
}
public void setLinkedList(LinkedList<Integer> linkedList) {
this.linkedList = linkedList;
}
public void deleteItemsInRange(int lo, int hi) {
for (Iterator<Integer> iterator = linkedList.iterator(); iterator.hasNext(); )
{
Integer value = iterator.next();
if(value.intValue() >= lo && value.intValue() <= hi)
{
iterator.remove();
}
}
}
public void printList() {
for (Integer integer : linkedList) {
System.out.print(integer.intValue() + " -> ");
}
System.out.print(" null");
}
}
import java.util.Iterator;
import java.util.LinkedList;
public class MainClass {
public static void main(String[] args) {
LinkedList<Integer> initialList = new LinkedList<Integer>();
initialList.add(4);
initialList.add(3);
initialList.add(7);
LinkedListDemo llDemo = new LinkedListDemo(initialList);
llDemo.deleteItemsInRange(3, 4);
llDemo.printList();
}
}
class LinkedListDemo {
private LinkedList<Integer> linkedList;
public LinkedListDemo(LinkedList<Integer> linkedList) {
super();
this.linkedList = linkedList;
}
public LinkedList<Integer> getLinkedList() {
return linkedList;
}
public void setLinkedList(LinkedList<Integer> linkedList) {
this.linkedList = linkedList;
}
public void deleteItemsInRange(int lo, int hi) {
for (Iterator<Integer> iterator = linkedList.iterator(); iterator.hasNext(); )
{
Integer value = iterator.next();
if(value.intValue() >= lo && value.intValue() <= hi)
{
iterator.remove();
}
}
}
public void printList() {
for (Integer integer : linkedList) {
System.out.print(integer.intValue() + " -> ");
}
System.out.print(" null");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.