Resolved Linked list is not updated

yakg5000

New member
Joined
May 30, 2021
Messages
2
Programming Experience
3-5
Hey, I wrote the following code but there's something I miss since nothing changes the pointers of the list for some reason.
My idea in the method is to remove all duplicated nodes, so in 6 6 6 3 2 2 1, should be changed to 6 3 2 1
As mentioned nothing changes when I am changing the reference points.. So setting the next reference seem to not really change it..
Thanks.
C#:
public int removeDuplicates(Node head)
{
    Node current = head;

    LinkedList result = new LinkedList();

    while (current != null)
    {

        Node runner = current.next;

        while (runner != null)
        {
            while (runner != null && runner.data == current.data)
            {
                runner = runner.next;
            }

            runner = runner.next;
        }
        result.AddLast(current.data);

        current = current.next;

    }

    return result.count();
}
 
Last edited by a moderator:
I haven't tested your code but the first thing that stands out is that you are creating a new LinkedList object and adding modes to it. If you expect the original LinkedList to change, you probably ought to change it, rather than adding to a new one.
 
Have you actually debugged the code? If not, you should have. You should have actually watched the object(s) that you expect to change and see whether they do when you expect it. If they don't, you should be able to point out to us exactly what happened and where.
 
Thanks, I realized I didn't create a simple link between current and runner pointers, a simple connection solved it.
Another question is what would be the best solution if the request is not to change the original list? Would duplicate the list beforehand do the trick?
 
Another question is what would be the best solution if the request is not to change the original list? Would duplicate the list beforehand do the trick?
I would assume that that is already what you were doing. You just need to get the list you created in the method instead of just its item count.
 
Back
Top Bottom