Dirk Gently's Holistic Detective Agency owes a lot to two Doctor Who episode Douglas Adams wrote for the BBC called "City of Death" and "Shada".

Regarding Adams' Borrowing from City of Death

In this episode, the last of an alien race called the Jagaroth goes back in time to a primordial earth to stop the explosion of a Jagaroth ship. The energy from the explosion of the Jagaroth ship juiced up earth's amino acid soup and kick started life on earth. If the Jagaroth ship did not explode, life on earth would never have developed. Doctor Who had to go back in time with a bumbling, oafish pulp-era detective named Duggan to make sure the ship blew up. Sound a wee bit familiar?

The proto-Dirk Gently, Duggan (played by actor Tom Chadbon) had a penchant for hitting people and smashing bottles.

Duggan: You can't make an omelet without breaking eggs!

Romana: If you made an omelet, I'd expect to find a pile of broken crockery, a cooker in flames, and an unconscious chef!

Curiously, of all the Doctor Who episodes ever made, "City of Death" was one of the few that never had a follow-on novelized version. The writing credit is given to David Agnew, which was a pseudonym used by Douglas Adams when he co-wrote with Doctor Who script editor Graham Williams. Apparently, Adams wrote the bulk of the script and refused to let anyone tackle the novel version. Well, now we know the novelized treatment Adams intended. "David Agnew" is also credited on the "The Invasion of Time" episode. The only aired Doctor Who Adams wrote completely was "The Pirate Planet". Williams is solely responsible for "The Nightmare Fair".

Many considered "City of Death", first aired in 1979, one of the best Doctor Who episodes ever made. Many at the BBC resented Adams' attempts at joking up Doctor Who. They feared he would turn it into an American-style sit com. However, Adams managed to sneak a lot of his humor into this one, including some rarely seen romantic moments between the Doctor and Romana. It was certainly one of the highest rated Doctor Who episodes, owing to a strike at ITV. The episode even featured a funny cameo by John Cleese, who played an art critic.

Regarding Adams' Borrowing from Shada

Having never seen "Shada" (the episode was never completed due to a strike), it's very hard to say what Adams' borrowed from this Doctor Who script. If you saw "City of Death" and then read Dirk Gently's Holistic Detective Agency, you definitely went "hey, that ending is right out of the Jagaroth episode! Poo! Poo!" Googling for detail about "Shada" and an analysis of similarities between the two works turns up little actual information. There are a whole lot of pages that state with great authority that "Well, as all the cool people know, Dirk Gently's was totally borrowed from 'Shada'." But no one seems to know why this is the case. It's much like the underwear stealing gnomes from Southpark. All the gnomes know Phase 1 (steal underpants) and Phase 3 (profit) but they're pretty hazy on Phase 2 (????).

As best I can tell, Adams used some settings and dialog from "Shada", notably the episode being partially set at Cambridge. As well both featured a Cambridge professor named Chronotis. In both book and episode, each has an apartment at Cambridge that is really a time ship. In the "Shada" episode Chronotis is a Timelord and Doctor Who's former teacher. In Dirk Gently's Holistic Detective Agency, Dirk is a former student of Cambridge.

Regarding Adams' Borrowing

Some fans are upset that Adams reused so much material, especially reusing a story ending many of his fans already saw. Dirk Gently's Holistic Detective Agency was written in a period of Adams' life when he saw a large part of his money made from Hitchhikers embezzled and lost by a corrupt accountant. One posits this first Dirk novel was written somewhat hastily as a bit of a cash grab, an attempt to recoup some of the hard-earned money he lost.

When writing Life, The Universe, And Everything Adams borrowed from a Doctor Who story outline he wrote called "Doctor Who and the Krikkitmen". Adams pitched "Doctor Who and the Krikkitmen" as the basis for a full blown Doctor Who movie but the idea never got beyond the treatment stage.

Regarding Adams' Borrowing from the "Stolen Cookie" Urban Legend

There's also some controversy about Adams borrowing from the "stolen cookie" Urban Legend, including it in his So Long and Thanks for All the Fish novel, and then passing it off on talk shows as having actually happened to him. The well-known Urban Legend goes something like this:
Before a man gets on a train he decides to buy a small bag of cookies for a snack. He finds an empty train car and sits down. At the next stop this Indian fellow gets on and joins him in his car. The Indian man looks kind of fresh off the boat, a definite "wet back", and he's a bit suspicious of this new arrival sitting across from him. After a bit of eyeing the Indian guy, he decides to open his packet of cookies on the table between them. He takes one. Much to his horror, the Indian fellow, with almost surgical precision, reaches out, snatches a cookie from the bag, and eats it! The nerve! The man takes another cookie. The Indian again helps himself to another. The man begins to give the Indian guy the dirtiest of looks and his mind begins to seethe with anger that these Indian wet backs don't understand the concept of personal property. He takes another cookie, quickly followed by the Indian. This goes on until there's one cookie left. The Indian fellow waves an opened hand at the remaining cookie, making the universal "please, help yourself to the last cookie" gesture. The man eats his last cookie, fuming not only at this Indian's ill mannered nerve but his spiteful magnanimous gesture. The train finally arrives at the man's station. He gets up, collects his things, and then before he leaves the car he says to the Indian man in a very sarcastic tone, "Well, I sure hope you enjoyed those cookies!" The man gets off the train. He fumbles through his pockets to find his wallet so he can rent a car. And what does he find in his pocket? The packet of cookies he originally bought!

Many people familiar with Urban Legends thought it was a bit weird Adams would include a well-known tale in his book. However, when he started going on shows like the Tonight Show claiming the inspiration for that fictionalized account really happened to him, people started calling Adams to task.

Adams defended himself claiming that it really did happen to him. This is how the Urban Legend started. Adams explains in an email dated November 1993 (back in the day when a celebrity could use the Internet and expect some civility out of users):
It happened to me on Cambridge station in the summer of 1976. A couple of years later I told the story on the radio, and then subsequently began to come across all sorts of variations of the story cropping up all over the place. I don't really care whether you believe me or not. I'm simply relating what occurred. If you are predisposed to believe - on no evidence - that I am a liar, there's not a lot I can do about that.

While it's possible it happened to Adams in real life, folklorists were documenting the legend several years before Adams claimed it happened. If it did happen to Adams, he is at least incorrect in his belief that he was the legend's progenitor.