I have occasionally practiced at what I call "Chop Shop Songwriting". You take an existing melody that has a "feel" you like, or fits the lyrics you have, then change the body lines, file off the serial numbers, and give it a new coat of paint. Voila! New song.
By "change the body lines...", I mean you start with the existing tune's basic structure (chord progression, where the melody goes higher or lower, where there are significant jumps in rhythm or pitch, etc). Then tweak those so it's a different tune (ascending notes vs. descending, make the interval between two notes into a fifth rather than a third, swing it from 4/4 time into 6/8, whatever). Sometimes you can take a printed score & turn it upside down to come up with something new - I have heard a legend that the main theme from the first Star Wars movie is based upon "Born Free" in this fashion. And don't the opening bars of "The Ballad of Gilligan's Island" sound just a wee bit like "Spancil Hill"?
1. be sure to change things enough so there is no doubt that it is a different composition from the original. You must apply some creativity if you want to claim something as your own.
2. you still have to apply a sense of aesthetics to the result, and tweak it until it sounds good.
This is sort of codifying and structuralizing (is that a word?) the process of taking inspiration from an existing work. How many songwriters can truthfully claim they never incorporate melodic ideas they have heard elsewhere? Most of us listen to music we think is "good" and try to make something that is "as good".
Is this the best way to create new and unique melodies? Maybe not - but it can be a fun and perhaps useful exercise for developing the CRAFT of songwriting to go along with the ART of songwriting. It also helps to develop a recognition of what elements make a melody "work". Inspiration is a fine and necessary thing, but most of the time it must be coupled with some skill to bring the inspiration into reality. Just as performance benefits from practice, so too does writing.