How to delete list length - n amount of minimums from a DLL

//The new DoubleLL should have the n largest nodes in the SAME ORDER as the original SingleLL ​​ public static DoubleLL TopNodes(SingleLL list, int n) throws Exception { DoubleLL output = new DoubleLL(list); output.resetCursor(); boolean listEnd = !output.advanceCursor(); while(n < output.listLength()) { int min = Integer.MAX_VALUE; if(output.getNodeData() < min) { min = output.getNodeData(); } if(listEnd) { output.resetCursor(); while(n < output.listLength()) { if(output.getNodeData() == min) { output.Delete(); break; } else { output.advanceCursor(); } } } else { output.advanceCursor(); } } return output; } ​​ I have to complete this homework assignment in data structures, i have to remove an x amount of minimums so that the DLL becomes n long.
31 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @Polar! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot3mo ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
Polar
PolarOP3mo ago
Polar
PolarOP3mo ago
this is the other class im using
Madjosz
Madjosz3mo ago
Do you have any idea how to do the task algorithmically (i.e. with pen and paper)?
Polar
PolarOP3mo ago
no clue
bassus
bassus3mo ago
//The new DoubleLL should have the n largest nodes in the SAME ORDER as the original SingleLL

public static DoubleLL TopNodes(SingleLL list, int n) throws Exception
{
DoubleLL output = new DoubleLL(list);
output.resetCursor();
boolean listEnd = !output.advanceCursor();
while(n < output.listLength())
{
int min = Integer.MAX_VALUE;
if(output.getNodeData() < min)
{
min = output.getNodeData();
}
if(listEnd)
{
output.resetCursor();
while(n < output.listLength())
{
if(output.getNodeData() == min)
{
output.Delete();
break;
}
else
{
output.advanceCursor();
}
}
}
else
{
output.advanceCursor();
}
}
return output;
}

//The new DoubleLL should have the n largest nodes in the SAME ORDER as the original SingleLL

public static DoubleLL TopNodes(SingleLL list, int n) throws Exception
{
DoubleLL output = new DoubleLL(list);
output.resetCursor();
boolean listEnd = !output.advanceCursor();
while(n < output.listLength())
{
int min = Integer.MAX_VALUE;
if(output.getNodeData() < min)
{
min = output.getNodeData();
}
if(listEnd)
{
output.resetCursor();
while(n < output.listLength())
{
if(output.getNodeData() == min)
{
output.Delete();
break;
}
else
{
output.advanceCursor();
}
}
}
else
{
output.advanceCursor();
}
}
return output;
}

I have to complete this homework assignment in data structures, i have to remove an x amount of minimums so that the DLL becomes n long. Not to be mean or anything, but it looks overly complicated, what you are doing here :/
Madjosz
Madjosz3mo ago
But I mean the idea is ok. It's O(n²) in complexity but it's not required by the task to have anything better. Currently this will fail because you are never updating listEnd (it is not reevaluated each time) and I would suggest to separate the minimum-finding logic from the deletion.
Polar
PolarOP3mo ago
no clue ngl i havent done java in 2 years before i got put into th is class
Madjosz
Madjosz3mo ago
Where did you get this code? Is it yours or some copy-paste/ai-generated stuff?
bassus
bassus3mo ago
Maybe let's start easy. How would you detect a Minimum in a linked list?
Polar
PolarOP3mo ago
i made it
Dexter
Dexter3mo ago
if(output.getNodeData() < min)
{
min = output.getNodeData();
}
{
min = output.getNodeData();
}
output.advanceCursor()
This message has been formatted automatically. You can disable this using /preferences.
Polar
PolarOP3mo ago
in the while loop
bassus
bassus3mo ago
exactly and why not just repeat that for (int i = 0; i < n; i++)? Or am i missing something?
Madjosz
Madjosz3mo ago
The task is to have the n biggest elements in their original order.
bassus
bassus3mo ago
Ah nice, i just can't read 😄 Sry
Polar
PolarOP3mo ago
dw about it
Madjosz
Madjosz3mo ago
I see basically two ways to achieve this: Either sort the list desc, find the n-th element and delete everything which is smaller from the original list, or repeatedly delete the smallest element until only n elements left. Both require O(n²) steps.
bassus
bassus3mo ago
Wait, why is the first n²?
Polar
PolarOP3mo ago
where
bassus
bassus3mo ago
Nah, not the n in your Code xD I am talking bout O(n²) 😂
Polar
PolarOP3mo ago
oh mb i believe it's n^2 because it traverses the linked list once to find the minimum then a second time to remove it although im noit completely sure
bassus
bassus3mo ago
Did you write this yourself?^^
Polar
PolarOP3mo ago
no that was given to me
bassus
bassus3mo ago
Nope, that ain't it^^ Okay, a little bit goofy, the copy method
Polar
PolarOP3mo ago
:shrugging:
bassus
bassus3mo ago
Yeah is not an issue, i just think it's interesting, that everything is deep copied, except the head-node xD Ah wait, bc we don't want to return that shit in descending or ascending order That is why it's not possible in O(nlogn)
Madjosz
Madjosz3mo ago
It's more like sorting a linked list is not so practical in O(n log n).
bassus
bassus3mo ago
Absolutely
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@267738692636901376>.

Did you find this page helpful?