Regarding the punishment in this world of the souls of wicked people, they have to reincarnate many times to rectify their sins, and there is almost no person who can avoid these gilgulim. After they die, the wicked enter Purgatory and receive punishment there to atone for their sins, and their judgment lasts twelve months.

Their souls get pushed from gilgul to gilgul until their sins have been cleansed a little.

There are wicked people of whom the verse says, "And may He hurl away the soul of you enemies as one shoots a stone from a slingshot" (Samuel I 25:29); they don’t yet merit to enter Purgatory after their deaths to cleanse their sins. Rather, their souls get pushed from gilgul to gilgul until their sins have been cleansed a little[, just enough] to allow them to enter Purgatory for twelve months to atone. There is no set time for this, sometimes they can continue in gilgulim for ten, one hundred, or even one thousand years - depending upon to what extent they sinned previously.

Though Purgatory is not a place one looks forward to, once a person requires atonement it becomes a necessity. However, even to enter Purgatory one must not have slipped to too low a spiritual level because if he has, he will first require gilgulim to raise him to a level that can allow him to enter Purgatory and achieve his final atonement.

However, righteous people and Torah scholars can never be affected by the fires of Purgatory, as it is written regarding Elisha: "He cannot be judged because of his involvement with Torah". (Chagiga 15b) Therefore, they need to reincarnate in this world to cleanse their sins for, "There is no righteous person in the world who does good and does not sin" (Eccl. 7:20).

Torah-learning provides a special protection against the rectifying "flames" of Purgatory, which creates a problem for a Torah scholar in need of rectification. Therefore, he has no alternative but to complete all of his tikunim through gilgulim instead.

A righteous person after he leaves this world is able to ascend to great heights in the world-to-come, but not all at once. Immediately after his death they punish him to rid him of his more severe sins, after which time they place him past the first division. When the time comes to ascend to a higher partition, they return him [to this world] to receive more punishment, this time for the lighter sins, after which he can ascend to the second level.

Then he will finally be able to ascend to his true, fitting section in paradise.

After that, they return him again to be punished for the minutiae of the law - the 'hairsbreadth' that he did not carry out, according to the secret of, "His [closely] surrounding ones are [subject to] extreme turbulent [conditions]" (Psalms 50:3). Then he will finally be able to ascend to his true, fitting section [in paradise].

Completely righteous people like David1 or Daniel, G‑d wanted to make known that they were in the world-to-come and that they would not require neither punishment nor gilgulim. As it says, "Had I not trusted that I would see the goodness of G‑d in the land of life!" (Psalms 27:13), and, "One thing I asked from G‑d, this that I shall seek. That I dwell in the House of G‑d all the days of my life." (Psalms 27:4).

This was also stated by Abigail the prophetess (Megila) when she said, "The nefesh of my master is bound up in the bundle of life" (Samuel I 25:29). Of Daniel it was said, "As for you, go to your end [where] you will rest." (Daniel 12:13).