Image caption Breaking Bad is ending after five series of increasingly macabre storytelling

Cult US drama Breaking Bad draws to a close on Sunday night, as viewers find out whether chemistry teacher turned drug dealer Walter White finally has his comeuppance.

The level of anticipation is huge. Fans have spent most of the week trying to decipher clues from the last episode's title, Felina (an anagram of finale).

Creator Vince Gilligan is keeping his cards close to his chest. The only person outside the cast who has seen the last script is his girlfriend, he told Rolling Stone.

But ending a much loved, long-running show is a daunting task. Gilligan admits that when he wrote the pilot in 2005, he had no idea where the story would go, beyond the initial concept of "Mr Chips becomes Scarface".

"You'd be surprised to know how little I had figured out," he told BBC Radio 4's Front Row.

"The ending was something that was a bit mysterious to us all right up until a month or two before we finished shooting."

In anticipation of the series' conclusion, here are some of the shows that got their dying moments right, and the ones that failed spectacularly.

THE SOPRANOS

"I think a lot of people thought they were being made a fool of," said Sopranos creator David Chase, of the show's infamous non-ending.

Broadcast in June 2007, the show's 81st episode was called "Made In America". The last scene was set in a diner, filled with images of the American dream. Mafia kingpin Tony Soprano took a seat in a booth, selected Journey's Don't Stop Believing on the jukebox and waited as, one by one, his family arrived.

His daughter Meadow had been having trouble parking her car. As she finally arrived and pushed the door open, the screen faded to black.

Fans were furious. What did it all mean?

Even the leading man was annoyed. "When I first saw the ending, I said, 'What the... ?!'" actor James Gandolfini (Tony Soprano) told Vanity Fair last year.

"I mean, after all I went through, all this death, and then it's over like that?"

Then he added: "After I had a day to sleep, I just sat there and said, 'That's perfect.' "

Many thought Tony had dodged the executioner's bullet, although the prevailing theory - explained at great length online - is that he was shot dead by a mobster who had been hiding in the bathroom.

Chase has always been enigmatic about the ending: "I think that to explain it would diminish it," he once said.

But an interview last year shed more light on his thinking.

"Did Tony die or didn't he die? Well, first of all, it really comes down to this: There was, what, six seasons of that show? Seven? Am I supposed to do a scene and ending where it shows that crime doesn't pay? Well, we saw that crime pays. We've been seeing that for how many years?

"Now, in another sense, we saw that crime didn't pay because it wasn't making him happy. He was an extremely isolated, unhappy man. And then finally, once in a while, he would make a connection with his family and be happy there.

"But in this case, whatever happened, we never got to see the result of that. It was torn away from him and from us."

LITTLE HOUSE ON THE PRAIRIE

For more than a decade, viewers watched the Ingalls family tackle farm life on their 1800s homestead.

Over 184 episodes, they survived tornadoes, teenage pregnancies, typhus and train crashes - and that's just the T's.

But nothing could prepare viewers for the final episode, in 1984, when the residents of Walnut Grove decided to blow up the town.

The episode revolved around a ruthless robber-baron, Nathan Lassiter, who held the deeds to the town, and planned to build a railroad through it. In protest, the residents set the town with dynamite and sent it sky-high.

But the destruction was pragmatic, as well as dramatic.

TV company NBC had built their sets in the Simi Valley, north of Los Angeles, on land leased from the Getty Oil Company - and had agreed to restore the land to its original state when the show ended.

Actor Michael Landon, who wrote and directed the final episode, realised he could incorporate his contractual obligation into the story.

At the time, he called it "a nice catharsis for the cast and crew" but Melissa Gilbert, who joined the cast as a child star, remembers it differently.

"I grew up in and around... all these buildings," she told the Archive of American Television in 2011.

"I got my first kiss behind the church - for real, in real life. And it was just gone.

"It was crushingly sad for all of us."

I MARRIED DORA

I Married Dora was an obscure, unloved US sitcom, notable only for casting a 15-year-old Juliette Lewis in an early role.

The show revolved around a single father, Peter (Daniel Hugh Kelly), who married his Salvadorean housekeeper Dora (Elizabeth Pena) to prevent her deportation.

Created by The Cosby Show's Michael J Leeson, it lasted just 13 episodes before the axe fell.

In the dying moments of the final episode, Peter was seen boarding a plane to Bahrain, only to arrive back in the airport lobby moments later.

"It's been cancelled," he announced.

"The flight?" asked Dora.

"No, our series."

The cast then turned to face the camera and waved "Adios" to their audience, as the shot pulled back to reveal the set and the crew.

NEWHART

Wavy lines. Spooky music. "And it was all just a dream."

Dallas famously undid an entire series when Pam Ewing woke up to find her husband Bobby in the shower - a year after he supposedly died in a car crash.

Crossroads and The Brittas Empire both took place in the imagination of their lead characters, while one episode of Buffy The Vampire Slayer alluded to the fact that Buffy Summers was hallucinating her story from inside a mental institution.

It's a risky device, which can leave viewers feeling hoodwinked - but undoubtedly the best implementation was when the curtain fell on Bob Newhart's 1980s sitcom.

The show told the increasingly bizarre story of a New York author who moves to Vermont and opens an inn.

In its final scene, the comedian abruptly woke up in bed next to actress Suzanne Pleshette, who had played his wife 15 years earlier, in a separate sitcom called The Bob Newhart Show.

"Honey, you won't believe the dream I just had," he said, and the series bowed out to the theme tune of its predecessor.

LOST

Six years after the most expensive pilot in television history, 13.5 million people tuned in to watch the finale of Lost.

Two-and-a-half hours later, chat show host Jimmy Kimmel summed up their reaction: "That's it? Seriously? That's it? Come on. Really?"

The New York Times called the ending a "cop-out" while a fantastically sweary review on Gawker accused the writers of "cheating".

Why? Because after six series of suspense, it turned out the survivors of the show's initial plane crash were dead after all.

It wasn't quite that simple, though. Everyone died at different times, some on the island where they'd been stranded, others many years later.

The cast were reunited in Purgatory (or perhaps the after-life) for a final group hug, and the central mystery was resolved. The island was a cork, designed to contain an evil force so it didn't leak out and infect the rest of the world.

But there were dozens of narrative dead ends, questions and plot points fans needed resolved before they could sleep happy. They didn't get them.

"I make no apologies for it", said the show's creator Damon Lindelof last year. "It's the story I wanted to tell."

"It's not that I didn't care about the mythology of the show, it's just like many shows have come and gone that are very focused on their mysteries and their mythologies and their ambiguity and there is no worse scene in the history of genre than the Architect explaining to Neo everything that happened in The Matrix.

"I wasn't going to touch that with a ten-foot pole."

ST ELSEWHERE

A medical drama made by the Hill Street Blues team, St Elsewhere was set in St Eligius, an understaffed and overburdened Boston teaching hospital.

For the last episode in 1988, writers packed out the drama with endless in-jokes and strange patients, including a fat lady who needed treatment for her lost voice.

As soon as Dr Fiscus (Howie Mandel) cured her and the fat lady sang, the show was over.

Except, that is, for a spooky last scene in which the entire six years of the drama were revealed to have been imagined by Dr Westphall's autistic son, who was seen staring at a model of the hospital set inside a snow globe.

"I never approved of the last episode," said Norman Lloyd, who had played Dr Daniel Auschlander throughout the show's six-season run.

"There are people who think it was the most brilliant inspiration in the history of television. I thought it was a cheat."

But the story gets stranger still. Since St Elsewhere featured a crossover episode with Cheers and some characters later appeared in Homicide: Life on The Street, the boy must have imagined those shows and maybe more too.

"Someone did the math once," said producer Tom Fontana in 2002, "and something like 90% of all television took place in Tommy Westphall's mind. God love him."

BLAKE'S 7

By 1981, Blake (Gareth Thomas) and his gang of space rebels had been battling the evil Federation, led by the crop-haired siren Servalan (Jacqueline Pearce), for four Earth years.

Blake himself had been absent since the second series - but reappeared, disfigured, for the final episode posing as a bounty hunter.

He was shot by his arch-rival Avon (Paul Darrow) and then the rest of the crew died (in slow motion) in a bloody shoot-out with Federation troops.

Broadcast on 21 December 1981 the shocking finale earned writer Chris Boucher the title "the man who killed Christmas".

But the climactic scene was only intended to be a cliffhanger.

Had a fifth series gone ahead, Boucher said, Avon would have survived, and become the show's hero.

"Don't forget you never actually saw him die, did you?" he said. "The freeze-frame ending does leave open the possibility that he may have survived after all."

THE PRISONER

The premise of The Prisoner was simple. A secret agent (Patrick McGoohan) resigns from his job and is taken to The Village, a beautiful but mysterious idyll he is never allowed to leave.

Branded "Number Six", he spends the series trying to discover the identity of "Number One", the shadowy governor of his fate.

The denouement was baffling. Hooded figures danced in a mock court as someone sang "Dem Bones". McGoohan ripped off Number One's mask to reveal... a chimpanzee mask. He ripped that off as well, and found his own face, laughing back at him.

Supposedly an allegory on the role of the individual in society, it was deliberately surreal and utterly confounding. The revelation of Number One's true identity lasted less than a second, in an era before video recorders allowed viewers rewind and rewatch.

It has been suggested the show went off the rails because ITV pulled the plug and gave McGoohan one week to write the finale.

The more likely explanation is that script editor George Markstein, who had spent months curtailing McGoohan's more outlandish ideas, had quit.

Either way, McGoohan was vilified by fans and "hounded" out of the UK after the show was broadcast in February 1968.

"Walking around the streets, it was dangerous!" he told Canadian television in 1977. "They thought they'd been cheated. Because it wasn't, you know, a 'James Bond' Number One guy."

But he claimed to be "delighted" by the response. "I wanted to have controversy, argument, fights, discussions, people in anger waving their fist in my face, saying, 'How dare you?' "

"I was delighted with that reaction. I think it's a very good one. That was the intention of the exercise."

FRIENDS

"We didn't have a gimmick," said Friends creator Marta Kauffman of the series' 236th and final episode.

In an uncontroversial and heartwarming farewell, the show allowed its six characters to move out of New York, and into the next stages of their lives.

Monica and Chandler got their surrogate child, which turned out to be twins ("We only ordered one!"); Joey acquired a new duck; and Ross and Rachel got back together - after a mad dash to the airport in Phoebe's taxi.

The show ended with the cast putting their keys on the kitchen table and leaving Monica's apartment for the last time. "Has it always been purple?" asked Joey.

Kauffman said the writers had been bewildered by the task of wrapping the show up, spending several days staring at blank sheets of paper without writing a word.

But they finally cracked it - taking inspiration from The Mary Tyler Moore Show, in which the cast shared a group hug and filed out of the set, leaving Mary to turn off the lights and close the door.

More than 52 million people tuned in for the finale in the US, much to the delight of advertisers, who had stumped up $2.1m (£1.1m) for a 30-second slot.

"What we hope is that people feel good about saying goodbye to them, and that they're all going to be OK," Kauffman said.

LIFE ON MARS

After an accident, modern-day detective Sam Tyler (John Simm) woke up in 1973, when men were men, and men were also sexist alcoholics in bad trousers.

Over two series, viewers were left to wonder whether his predicament was real, or a fitful by-product of his coma.

In the final episode, Sam learned that corrupt cop Gene Hunt (Philip Glenister) was the "tumour" in his brain. If he could get rid of Hunt, he could go home. The operation (get it?) was nearly over.

Then, he was told the exact opposite: He really was in 1973, and suffering from amnesia after a car crash.

Eventually, the "real" Sam woke up in a hospital in 2006 - but the writers had one final twist in store. He decided he preferred the 70s after all, and jumped off the hospital roof, sending himself back into a coma, and back into the arms of his dream girl, Annie.

The Guardian called it "the perfect finale" but the Express declared the "shaggy dog story" was ultimately "baffling".

Still, it fared better than the short-lived US remake, where Sam turned out to be an astronaut on the first manned mission to Mars, trapped in a malfunctioning hibernation chamber.

The final episode of Breaking Bad is broadcast in the US at 21:00 EST on Sunday, 29 September. It will be available on iTunes and Netflix in the UK shortly afterwards.