Reverse a Linked List in C with Explanation

C Program to Reverse a Linked List

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *head = NULL;

struct node *createNode() {
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return newNode;
}

void insertNode() {
    struct node *temp, *ptr;
    temp = createNode();
    printf("Enter an element to insert in Linked List: ");
    scanf("%d", &temp->data);
    temp->next = NULL;
    if (head == NULL)
        head = temp;
    else {
        ptr = head;
        while (ptr->next != NULL) {
            ptr = ptr->next;
        }
        ptr->next = temp;
    }
}

void listReverse() {
    struct node *prev = NULL, *current, *nextNode;
    current = head;
    while (current != NULL) {
        nextNode = current->next;
        current->next = prev;
        prev = current;
        current = nextNode;
    }
    head = prev;
    printf("List reversed successfully\n");
}

void viewList() {
    struct node *temp = head;
    if (temp == NULL) {
        printf("List is empty\n");
    } else {
        printf("Elements in Linked List are: ");
        while (temp != NULL) {
            printf("%d ", temp->data);
            temp = temp->next;
        }
        printf("\n");
    }
}

int menu() {
    int choice;
    printf("\n 1. Insert an element in Linked List");
    printf("\n 2. Reverse the list");
    printf("\n 3. View/Traverse the list");
    printf("\n 4. Exit");
    printf("\n Please enter your choice: ");
    scanf("%d", &choice);
    return choice;
}

int main() {
    printf("**C Program to Reverse a Linked List**");
    while (1) {
        switch (menu()) {
            case 1:
                insertNode();
                break;
            case 2:
                listReverse();
                break;
            case 3:
                viewList();
                break;
            case 4:
                exit(0);
            default:
                printf("Invalid choice\n");
        }
    }
    
    return 0;
}

Output:

**C Program to Reverse a Linked List**
 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 1
Enter an element to insert in Linked List: 4

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 1
Enter an element to insert in Linked List: 7

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 1
Enter an element to insert in Linked List: 3

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 1
Enter an element to insert in Linked List: 9

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 3
Elements in Linked List are: 4 7 3 9 

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 2
List reversed successfully

 1. Insert an element in Linked List
 2. Reverse the list
 3. View/Traverse the list
 4. Exit
 Please enter your choice: 3
Elements in Linked List are: 9 3 7 4 
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now