Question: Need to complete the below removeMaximumValues ( int N ) function without using PriorityQueue: public class MyLinkedList { private static final long serialVersionUID = 1

Need to complete the below removeMaximumValues(int N) function without using PriorityQueue:
public class MyLinkedList {
private static final long serialVersionUID =1663679278942178557L;
static class Node {
private static final long serialVersionUID =-539394075146871892L;
String value;
Node next;
Node(String value, Node next){
this.value = value;
this.next = next;
}
Node(String value){
this(value, null);
}
}
protected Node head = null;
protected Node tail = null;
protected int size =0;
public void addFirst(String value){
Node newNode = new Node(value);
newNode.next = head;
head = newNode;
if (newNode.next == null){
tail = newNode;
}
size++;
}
public void addLast(String value){
Node newNode = new Node(value);
if (tail == null){
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
size++;
}
public void add(int index, String value){
if (index <0|| index > size)
throw new IndexOutOfBoundsException();
if (index ==0){
addFirst(value);
} else if (index == size){
addLast(value);
} else {
Node newNode = new Node(value);
Node current = head;
for (int i =0; i < index -1; i++){
current = current.next;
}
if (current.next == null){
tail = newNode;
}
newNode.next = current.next;
current.next = newNode;
size++;
}
}
public void removeFirst(){
if (head != null){
head = head.next;
} else {
return;
}
if (head == null){
tail = null;
}
if (size >0)
size--;
}
public void removeLast(){
if (head == null){// empty list
return;
} else if (head == tail){
// single element list
head = null;
tail = null;
} else {
Node current = head;
while (current.next != tail){
current = current.next;
}
tail = current;
current.next = null;
}
size--;
}
public void remove(int index){
if (index <0|| index >= size)
throw new IndexOutOfBoundsException();
else if (index ==0)
removeFirst();
else {
Node current = head;
for (int i =0; i < index -1; i++){
current = current.next;
}
current.next = current.next.next;
if (current.next == null){
tail = current;
}
size--;
}
}
/*
* Implement the methods below. Please do not change their signatures!
*/
public void reverse(){
/* IMPLEMENT THIS METHOD! */
if (head == null || head.next == null){
return;
}
Node prev = null;
Node current = head;
Node next = null;
while (current != null){
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public void removeMaximumValues(int N){
/* IMPLEMENT THIS METHOD! */
MyLinkedList list = this;
if(list == null || N <=0){
return;
}
//find the maximum
java.util.PriorityQueue maxValues = new java.util.PriorityQueue(java.util.Collections.reverseOrder());
Node current = head;
while (current != null){
if (!maxValues.contains(current.value)){
maxValues.add(current.value);
}
if (maxValues.size()> N){
maxValues.poll();
}
current = current.next;
}
current = head;
Node prev = null;
while (current != null){
if (!maxValues.contains(current.value)){
if (current == head){
removeFirst();
current = head;
} else if (current == tail){
removeLast();
current = null;
} else {
prev.next = current.next;
current = current.next;
size--;
}
} else {
prev = current;
current = current.next;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!