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 removeMaximumValuesint N function without using PriorityQueue:
public class MyLinkedList
private static final long serialVersionUID L;
static class Node
private static final long serialVersionUID L;
String value;
Node next;
NodeString value, Node next
this.value value;
this.next next;
NodeString value
thisvalue null;
protected Node head null;
protected Node tail null;
protected int size ;
public void addFirstString value
Node newNode new Nodevalue;
newNode.next head;
head newNode;
if newNodenext null
tail newNode;
size;
public void addLastString value
Node newNode new Nodevalue;
if tail null
head newNode;
else
tail.next newNode;
tail newNode;
size;
public void addint index, String value
if index index size
throw new IndexOutOfBoundsException;
if index
addFirstvalue;
else if index size
addLastvalue;
else
Node newNode new Nodevalue;
Node current head;
for int i ; i index ; i
current current.next;
if currentnext 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
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 currentnext tail
current current.next;
tail current;
current.next null;
size;
public void removeint index
if index index size
throw new IndexOutOfBoundsException;
else if index
removeFirst;
else
Node current head;
for int i ; i index ; i
current current.next;
current.next current.next.next;
if currentnext 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 removeMaximumValuesint N
IMPLEMENT THIS METHOD!
MyLinkedList list this;
iflist null N
return;
find the maximum
java.util.PriorityQueue maxValues new java.util.PriorityQueuejavautil.Collections.reverseOrder;
Node current head;
while current null
if maxValues.containscurrentvalue
maxValues.addcurrentvalue;
if maxValuessize N
maxValues.poll;
current current.next;
current head;
Node prev null;
while current null
if maxValues.containscurrentvalue
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
