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
⌛ This post has been reserved for your question.
Hey @Polar! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton 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.
Please format your code to make it more readable. For java, it should look like this:
this is the other class im using
Do you have any idea how to do the task algorithmically (i.e. with pen and paper)?
no clue
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 :/
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.no clue ngl
i havent done java in 2 years before i got put into th is class
Where did you get this code? Is it yours or some copy-paste/ai-generated stuff?
Maybe let's start easy. How would you detect a Minimum in a linked list?
i made it
if(output.getNodeData() < min)
output.advanceCursor()
This message has been formatted automatically. You can disable this using
/preferences.in the while loop
exactly and why not just repeat that for (int i = 0; i < n; i++)? Or am i missing something?
The task is to have the n biggest elements in their original order.
Ah nice, i just can't read 😄
Sry
dw about it
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.Wait, why is the first n²?
where
Nah, not the n in your Code xD
I am talking bout O(n²) 😂
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
Did you write this yourself?^^
no that was given to me
Nope, that ain't it^^
Okay, a little bit goofy, the copy method
:shrugging:
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)
It's more like sorting a linked list is not so practical in O(n log n).
Absolutely
Post Closed
This post has been closed by <@267738692636901376>.