A solution is posted here. Just a suggestion, try to find a solution yourself first. Start with designing an algorithm or a flow and then write your program. You'll be surprised to find that it'll be much simpler once you dig into it.
Also, if you find it difficult, then instead of searching for the solution directly, search for parts of the solution, for example, "Creating a node" or "Put data into a node", etc. This will increase your confidence.