You're essentially asking here:
Can I legally use code I found on a forum?
The short answer is 'it depends'. And by the way, IANAL/TINLA.
It depends on:
- whether the code is eligible for copyright,
- what license the content of the particular forum is under, and
- what additional license (if any) the individual contributor has put it under.
The code you've linked to comes from StackOverflow, a GitHub Gist, and a post in a GitHub issue.
The first thing you need to consider is whether the code is copyrightable at all. Quoting from a comment made by leezer3 on another post: "In order to be copyrightable (under UK/European law at least) the work must not be a simple mathematical algorithm, and must have unique properties." In other words, there needs to be something creative about it.
If it's simple enough, you can use it - no problem (I'd usually still include a comment linking to the post it came from though). If it's more complex, can you take the idea of the solution and completely rewrite it "in your own words"? If so, you now own the copyright on it (when doing this I still tend to comment with the original post as a sort of 'hat tip', but that's not required).
If it's not simple and you can't/don't want to rewrite it, then you need to consider how it's licensed on the website or forum you found it on. In the case of all StackExchange sites, scroll to the bottom where you'll see a link to CC BY-SA 3.0. You are free to use and change any content (code or text) that you find on SE sites, as long as you give credit and as long as you continue to share in the same manner any changes you make.
As far as giving credit, this blog post (also linked from the footer of every page) explains how StackExchange would like you to give that attribution. This post is written for websites who use SE content wholesale, so I tend to take a slightly different approach in code: I just ensure that I link in a comment to the original post so it's clear where it came from.
As far as sharing any adaptions in a like manner, this is where it can get a bit murky and where it's probably better to just rewrite what you find anyway. Depending on the license of your final product, you might be able to relicense your adaptions - see this answer for a path to license CC BY-SA content under the GPL.
In the case of GitHub Gists and Issues, anything posted on GitHub is owned by the original contributor and is not covered by a blanket license. So, it is automatically theirs unless they've given you a specific license to use it.
While I would assume someone posting code in a Gist or Issue would be happy for you to use it, to be safe you really need to ask them for permission. They might license it to you under the MIT or they might just say you can do whatever you like with it. Either way, crediting them with a comment linking to the Gist/Issue is the appropriate thing to do.
Finally, one additional consideration is whether the contributor has put their code under any additional license. On StackExchange, some users say in their profile that their code is also available under the Apache or MIT licenses, or released into the public domain. On GitHub, look for a comment or message to a similar effect on the user's profile.
If in doubt, you can try to contact that user directly and many will be happy to give you additional permissions.
In answer to your additional questions:
Are there any legal differences where ... the initial piece of code was written by me and then other forum users corrected/updated it?
Possibly. Going back to whether the code is eligible for copyright, if the updates were wholesale contributions that are copyrightable on their own, then technically the user who made those changes owns the copyright, and you would need to apply the guidelines I went through above if you want to use those contributions.
Are there any legal differences where ... I just asked a question and got an answer (a piece of code)?
No. Whether or not you asked the question and got a piece of code in response makes no difference to any copyright and license that may or may not be on that code.
One final consideration: if you use code you find on a public forum, you are still responsible for ensuring you are using it correctly. You'll want to be very sure that the person who posted code you use is really the copyright owner of the code, and that they didn't just copy it from somewhere else. Because of this, to be safe, you might want to just take most forum posts as 'suggestions' and use the ideas to write the code yourself.