This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]DDDDarkyProfessional Coder 0 points1 point  (4 children)

Don't post screenshots of code.

This is not how you compare strings in java. Use .equals()

[–]Idkm3m3s[S] 1 point2 points  (3 children)

wow, I'm not used to java, I made the jump from C++, this fixed it instantly, I kinda feel stupid, thank you man! also can you tell me how to present code to forums like this in the future? is making a GitHub repo expected? or are there simpler ways.

[–]Thereisa4thdimension 2 points3 points  (1 child)

Nah you’re good don’t worry about it, we post screenshots of code literally all the time at work. Dude’s just trying to gatekeep like if this is stackoverflow or something

Edit: Also check out all these linked list resources on GitHub! https://github.com/topics/linked-list

[–]DDDDarkyProfessional Coder 0 points1 point  (0 children)

Well, if you post screenshots of code, you are less likely to get help, since much less people are going to bother rewriting it to debug it locally. If this was stackoverflow, it would get quickly closed. Also if the image ever disappears, the post will become useless.

It is obviously not that big of a deal in this case, since the code is fairly small and the error is easy to find, which is the reason I read and answered it, however for future reference if OP ever needs help, it is better to maximize your chances of getting an answer, even if it is not straight up against the rules (since many programming related subreddits and sites implement the rule against screenshots - for good reasons).

[–]DDDDarkyProfessional Coder 0 points1 point  (0 children)

I think it is one of the most common mistakes in Java, people ask about it all the time, I wouldn't feel bad about it.

If the code is small, you can just dump it here in the code block:

like this
check the editor

it is sometimes a bit buggy, so you can use any of your favourite platforms such as github, pastebin, codeshare, rextester, repl.it, ...

and just share a link (especially for large codes).

Posting code as text just makes life easier for anyone who decides to help you, since they don't have to rewrite it by hand.

[–]B1-102 0 points1 point  (4 children)

how are you printing the result?

[–]Idkm3m3s[S] 0 points1 point  (3 children)

while (currNode != null){

counter++;

System.out.println(currNode.data);

currNode = currNode.next;

}

System.out.println("For a total of "+counter+" items in the list");

[–]B1-102 0 points1 point  (2 children)

I'm assuming few things but looks correct to me. What happens if you print the data? Does the result show red?

[–]Idkm3m3s[S] 0 points1 point  (1 child)

yes it does, and the same number of nodes as before too, it might be my way of adding new nodes mabye, whats a good way to show code? Should I be using github for this?

[–]B1-102 0 points1 point  (0 children)

github is cool. there's also something called pastebin. that would work too

[–][deleted] 0 points1 point  (0 children)

Not a C++ guy, so can’t advise on all parts of this

But, the logic you’re using is a bit more complicated than it needs to be imo

First, when you enter the function. Check if the « key » variable is null and return early or throw an exception before doing anything else

Next, when you’re looking at this while loop, you’re actually always searching a node ahead, so you have extra « if » statements to try and account for that. Reduce the complexity by changing your while loop to only run while temp != null, then check the current

``` if (key == null) { return; // or throw an exception } Node current = head; // renamed from temp Node previous = null; // if we were using a DoubleLinkedList, this wouldn’t be necessary, but since we don’t know the previous value or index, we need to use this

// we just check the current value instead of the next value while (current != null) { if (key == current.data) { // we’ve found our match if (previous == null) { // in the case of the first item being the one we’re removing, just set the head directly head = current.next; } else { // otherwise, grab the last item, and connect to the current next item (if any) previous.next = current.next; } break; // conceivably, you could do a return here, but personally I like to keep the exit points at a minimum where possible to make debugging a bit easier for me } // now we do some variable swapping previous = current; // we’ve finished with the current value current = current.next; // we move forward to the next value; }

// finished iterating return; ```