Reverse a Linked List in C – Dead Mad, Innit

reverse a linked list in c – dead mad, innit

intro

Oi, you lot! New to C and fancy summat a bit bonkers? We’re gonna sling together a lil program to reverse a linked list—ya know, them chains of boxes where we flip the whole lot so the last one’s first and all that. It’s dead tricky but proper ace—like when I was tryna reverse my mate’s conga line at the pub (ended in a heap). Great for getting your head round pointers in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on your blog. take a gander:

#include
#include

struct Node { // lil box, innit
int data;
struct Node* next;
};

void showList(struct Node* head) { // show the chain
struct Node* temp=head;
if(temp==NULL) {
printf("oi, list’s empty!\n");
return;
}
while(temp!=NULL) { // keep going til end
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}

struct Node* reverseList(struct Node* head) { // flip it!
struct Node *prev=NULL,*curr=head,*next=NULL; // three mates
while(curr!=NULL) { // loop til nowt
next=curr->next; // stash next
curr->next=prev; // point back
prev=curr; // shuffle up
curr=next; // move on
}
return prev; // new head!
}

int main() { // nearly did void, ha!
struct Node* head=NULL; // start with nowt
struct Node* second=NULL;
struct Node* third=NULL; // three lil pals

head=(struct Node*)malloc(sizeof(struct Node)); // nab space
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));

head->data=10; // chuck numbers in
head->next=second;

second->data=20;
second->next=third;

third->data=30;
third->next=NULL; // end it

printf("before flip: ");
showList(head);

head=reverseList(head); // do the flip

printf("after flip: ");
showList(head); // forgot ; once, ugh!

free(head); // tidy up
free(second);
free(third);

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I hard-coded a few numbers—worked bang on, mate!

before flip: 10 20 30
after flip: 30 20 10

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!

1. them `#include` bits
This is me nicking my toolkit. `` for `printf`, `` for `malloc` and `free`—fancy pants “memory grabbers”. Forgot the second one once, and my code just sat there like a plonker. Total mug move!

2. `struct Node` malarky
`struct Node` is my lil box—`int data` for the number, `struct Node* next` for the pointer to the next box. Like chaining mates in a line, innit!

3. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!

4. boxes: `struct Node* head=NULL;`
These are my lil pals—`head`, `second`, `third`—all pointers to `Node`s, starting at `NULL` coz we ain’t got nowt yet. `head`’s the start of the chain!

5. making the chain: `malloc` and stuff
`head=(struct Node*)malloc(sizeof(struct Node))` nabs memory for a `Node`—like renting a lil box. Fill it with `head->data=10` and link it to `second` with `head->next=second`. Same for `second` and `third`, til `third->next=NULL` ends it. Like chaining beer cans!

6. showing it: `void showList(struct Node* head)`
`showList` takes `head` and wanders through. `struct Node* temp=head` is my lil walker—if it’s `NULL`, “list’s empty!” Else, `while(temp!=NULL)` loops, printing `temp->data` and hopping to `temp->next`. `\n` keeps it tidy.

7. flipping it: `struct Node* reverseList(struct Node* head)`
Here’s the guts! `reverseList` uses three mates: `prev`, `curr`, `next`. `prev=NULL`, `curr=head` starts at the front, `next=NULL` for stashing. `while(curr!=NULL)` loops: stash `next=curr->next`, flip `curr->next=prev`, shuffle `prev=curr` and `curr=next`. When done, `prev`’s the new head—like turning a conga line back to front!

8. chucking it out: `printf` and pals
`printf("before flip: ")` and `showList(head)` show the start, then `head=reverseList(head)` flips it, and `printf("after flip: ")` with `showList(head)` shows the result. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

9. `free` and `return 0` to scarper
`free(head)` and pals tidy up—don’t wanna leave a mess! Then `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to flip a linked list. Faff with it—chuck in daft numbers, tweak it to say “flipped it” if ya fancy. First time I ran this, I forgot the flip and it stayed the same—duh, me! You’re a star already, mate—keep smashing it! 😊