Early in The Hobbit, in the Misty Mountains, Bilbo and the Dwarves are stuck in a storm in the mountain, and bored giants throw boulders at them. This episode plays a relatively minor part in the plot of the book, but I have heard many online discussions about it. The problem is, that in Tolkien's other works, and unpublished papers, he lays out a very careful order of what type of creatures exist in Middle Earth, informed by his Catholic beliefs. But Tolkien was also working with popular mythology, and in the case of The Hobbit, he was originally writing a small story unrelated to his larger body of work. As time went on, he (or sometimes his fans) had to reconcile the folk mythology was the epic, theological underpinnings. So there has been a lot of talk about whether the mountain giants, so briefly mentioned in The Hobbit, have souls, or were fallen angels, or some other such entity.
If you have read this far, you will notice that I have not mentioned anything about Ghosts or Harry Potter yet. The situation in the Harry Potter series is very close to this: J.K. Rowling started with various old folk legends and tales, that she wove together to use as background material for the Harry Potter series. Therefore, at the beginning of Harry Potter, she fills Hogwarts School of Witchcraft and Wizardy up with various floating ghosts and spirits, including Nearly Headless Nick and The Bloody Baron, and Moaning Myrtle. These ghosts are played mostly for the comedic effect, such as when Nearly Headless Nick is barred from a club of headless ghosts because his head isn't totally cut off.
But J.K. Rowling is not writing a childish story in Harry Potter. She has her own "theological" underpinnings, just like Tolkien did (although we don't know yet what her specific dogmas are), and she has rules in her world. One of those is that those who are dead are well and truly dead and can not "magically" come back to life. (Although in her world, just as in ours, people can believe to be dead, or can be brought back from the brink of death). And yet, with that rule in place, we obviously have clear and open references to ghosts, creatures who are obviously, in some form or another, surviving death. This is strange to us, and also strange to some of the characters in the book: In Harry Potter and the Order of the Phoenix, after Harry Potter's godfather Sirius Black has died, he decides to go and ask a ghost if he can get him in contact with Black. The ghost explains that very few wizards will have bothered to become ghosts, and that ghosts are not truly dead, and are merely living "a feeble imitation of life". Beyond that, the exact status of ghosts is not exactly specified, besides that they seem to be neither part of the world of life or death.
And yet, they seem to be there, and seem to be sentient. It is possible that they are a type of "artificial intelligence", able to respond but without sentience. Photographs and paintings in Harry Potter seem to behave this way, but this is not beyond what someone could currently do with flash. Or it could be that ghosts are indeed sentient, but are somehow lacking a soul, spirit, or other animating force. It does seem that the ghosts in Harry Potter are not automations, since they seem to exhibit feeling and opinions. It could be that they are sentient, but only within certain limitations, to "walk palely where there living selves once trod", as Nick expresses it. In other words, the ghost is still there and sentient, but the animating force that enables it to grow is gone, it can only reexperience what it has already seen.
The matter of ghosts in Harry Potter is important for two reasons: first, it shows us how Rowling integrates older, less structured folk mythological elements into her serious worldview, and second, it reveals to us possible clues about her basic view of such things as humanity and spirituality, views that may be further revealed to readers in the final book, or that may forever be discoverable through hermeneutic readings.