📘 Lesson  ·  Lesson 65

Reverse Linked List

Reverse Linked List

Reversing a Linked List

💡 Note

To reverse a linked list, we change each node's next pointer to point backwards, using three pointers.

Program

C
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };

struct Node* reverse(struct Node* head) {
    struct Node *prev = NULL, *curr = head, *next;
    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;   // reverse the link
        prev = curr;
        curr = next;
    }
    return prev;   // new head
}

Summary

  • Use three pointers (prev, curr, next) to flip each link.
  • The old last node becomes the new head.

Linked List Reverse करना

💡 Note

Linked list reverse करने को हम तीन pointers से हर node का next pointer पीछे की ओर करते हैं।

Program

C
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };

struct Node* reverse(struct Node* head) {
    struct Node *prev = NULL, *curr = head, *next;
    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;   // link reverse करें
        prev = curr;
        curr = next;
    }
    return prev;   // नया head
}

सारांश

  • तीन pointers (prev, curr, next) से हर link पलटें।
  • पुराना last node नया head बन जाता है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n