❔ Removing dupes in a linked list
I need to make a function that gets a linked list, and returns that linked list with all sequences of 3 or more consecutive equal numbers be removed
for example: 1,3,3,3,4,5,5,6,9,9,9,9
becomes
1,4,5,5,6
129 Replies
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
well i dont have any code written as i dont have an idea as to were to start
sadly
thats also my issue
alright
let's break down the problem
I have a very vague idea but no more than that
gotta start somewhere
what is your vague idea?
so
first thing is
p1 being a pointer on first object p2 on the one after
and inside that if clause i would put it into a new list
hold on
ill write it so its more clear
so
A) your plan is to build a new list, rather than trying to modify the existing one
yeah thats what they want in the question, sorry i didnt specify
great
although it really doesnt matter since i can make a copy of the old list i have a function for that
you're allowed to use the built-in
LinkedList
here, or you have to write your own?nope we use the one the teacher built
i can send you it if you want
please do
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
srry its long
lovely
very
i feel like i need a nested loop here
or another function that i build
possibly
there's a variety of ways you could do this
let's think about the problem first
Can i tell you the idea i thought of just now with the nested loop ?
you have to build a list from a set of input numbers
or do you wanna do this first i might have overlooked something
well i just like
i copied the example it gave me
right
where it should ignore any sequences of the same consecutive number, longer than 3
so
yep
what are you going to need here
that you KNOW
ok so
definitely a counter
maybe
even more fundamental than that
do you not think so?
um
not sure
while loop?
some kinda loop, for sure
to iterate the input numbers
yeah
and to make sure i dont reach null right?
what does "reach null" mean?
i mean like
the list ends at some point
which list?
any list i gues
s
i mean like after the 9 it points to null
nnnno, that array does not have any nulls in it
nonono not the array i create a list with the values from the array
i can send you that function as well if you want
okay, but what does that have to do with what we're talking about
some kinda loop, for sure to iterate the input numbers
ok yeah
i understand why
there's no nulls involved in iterating the input, if the input is an array
it isnt an array
its a linked list
the input is?
no look here i made a list out of the array
and then i call the function on the list
why?
idk thats what they want me to do
okay
so
the input is, in fact, a linked list
ye
and we are trying to build a new list from that
yes?
yeah
so, again
let's start with what we KNOW
So
and then we said, we KNOW we need to at least iterate all the nodes in the input list
so go ahead and write that loop
ok so
like a while loop or for loop
I would do likeeee
is that ok or would you do something different
for clarity, I would recommend
A) not modifying input parameters, just in general
B) not using a variable named
listHead
to represent something that isn't the head of a listso make a copy first?
oh
yeah that seems better
clarifies your intention
so
what else do we know?
about this method
um
that we have to compare values ?
true
what else?
ummmmm
well i have to create a new list
even more specifically, we have to return it
oh
yeah right
so like
yeah ok
wait what is that question mark
indicates that the value can be null
wait what does that mean
sorry
like what if i just do that without ?
thats what ive been doing
what version of .NET/C# are you using?
ummmm
i think 4.7.1
(look at your .csproj)
wait lemme check
um\
in here
close enough
you're on .NET Framework
oh
yeah
sigh why do universities insist on teaching people exlusively on out-of-date technology
im in highschool :(
and its not my teacher its like
the ministry of education
so, ignore the nullability thing, that was introduced to C# only somewhat recently
and it's an optional feature, anyway
kk
alr
so now, we have a basic structure for the method
what next?
I would like
try to see how we compare the values
while putting them into the new list
compare what values?
well each time we compare different values
each time what?
think about it more fundamentally
you have a loop
each iteration we compare 2 different values
over each item in the input list
yeah, what is it that we need to DO within each iteration?
so
well a couple of things right?
each iteration we compare 2 different valueswhich 2 different values?
um
like say the linked list is
1-> 2 ->2->3
then like
1st iteration: 1,2
2nd: 2,2
3rd: 2,3
idk how to put it into words
so, from the perspective of a single iteration
what are we comparing
ummmmmmmm
the value of currentNode and the next current node
that'll work
you could also consider it "the current node and the previous node"
rightttt that seems better
maybe
both are valid
alr
so
we add another variable?
or do we use current node
which approach are you going with?
i think ill go with previous and current
it seems easier
so, how do we retrieve the previous node, from the current node?
oh well i guess we cant really
righto
at least not in this linked list its not an option for us
not directly, yes
um
i have an idea
so before the loop we add another variable
prevNode
hold ill just write it down
would this be fine? or is there something better
well
that's wrong, for starters
well
no
that would work
assuming you always HAVE a first node
NOW it's wrong
like ur saying i might have a list with 1 value?
or no values?
you tell me
can those things happen?
well yeah i guess
how would your code handle it?
crash
like not run
no, not exactly
what happens if
listHead
is null
?then this wouldnt work right?
yup, that'll throw
NullReferenceException
hmmm ok so how do we fix that
lemme think
what if
listHead.GetNext()
is null
?do we just add an if?
well it would run
you can
indeed, it'll run completely fine for a list with 1 item
but it wouldnt even go into the loop
right
I mean, except for the fact that we aren't doing anything to build the new list, yet
well yeah
I gotta go make dinner, fill me in on your progress when I get back
alr
ill try
ty i really apprecaite it
ooooo i have an idea
on how to fix the NullReference thingy
and i might have gotten an idea for the nested loop but idk
i gotta see first
like i wanna use a previous and a next and a current
maybe that might work
but only initalize the next node in the loop
so i can like
i wanna use nextNode
to iterate inside the loop
until the thingy changes
then i can change currentnode to next node
i also think it might be better to delete thingies cuz idk how to do it otherwise
ima try what io have in imnd and come back
and like use previous to link the thingies
idk how to explainw ith words
ok so i think i got it
maybe i have a mistake
but what i did kinda like
links next node and prev node
and like
if it doesnt i guess i need to change prev current?
but only if it doesnt?
wait maybe not to next but to current?
ok i think i might have got it
i really hope this works cuz i gotta go sleep its 2:30 am
doesnt workkkk
mannnnn
i needs go to sleep i got school in 5 hours
Thank you so mcuh for the help i really really really appreciate it
ill go over this tmrw
again tysm
good night
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.