Knowee
Questions
Features
Study Tools

Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Write a function to create a new node with the given data // Write a function to print the linked list // Write a function to reverse the linked list recursively // Write your main function here

Question

Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h>

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

// Write a function to create a new node with the given data

// Write a function to print the linked list

// Write a function to reverse the linked list recursively

// Write your main function here

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the C program to reverse a single linked list recursively:

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

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

// Function to create a new node with the given data
struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

// Function to print the linked list
void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

// Function to reverse the linked list recursively
struct Node* reverse(struct Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct Node* rest = reverse(head->next);
    head->next->next = head;
    head->next = NULL;
    return rest;
}

// Main function
int main() {
    struct Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);

    printf("Given linked list\n");
    printList(head);
    head = reverse(head);
    printf("\nReversed Linked list \n");
    printList(head);

    return 0;
}

This program first creates a linked list with nodes 1, 2, 3, 4. Then it prints the original list, reverses it using the recursive function, and prints the reversed list.

This problem has been solved

Similar Questions

Write a C program to reverse a single linked list recursively. Sample Test Cases Test case 1 No·of·nodes:·5 Data·for·node·1:·5 Data·for·node·2:·4 Data·for·node·3:·3 Data·for·node·4:·2 Data·for·node·5:·1 Original·linked·list:·5·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·5·->·Null⏎ Test case 2 No·of·nodes:·7 Data·for·node·1:·1 Data·for·node·2:·2 Data·for·node·3:·3 Data·for·node·4:·4 Data·for·node·5:·3 Data·for·node·6:·2 Data·for·node·7:·1 Original·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎

Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Write a function to create a new node with the given data // Write a function to print the linked list // Write a function to reverse the linked list recursively // Write your main function here

he following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function./* Link list node */struct node{    int data;    struct node* next;};  /* head_ref is a double pointer which points to head (or start) pointer   of linked list */static void reverse(struct node** head_ref){    struct node* prev   = NULL;    struct node* current = *head_ref;    struct node* next;    while (current != NULL)    {        next  = current->next;          current->next = prev;           prev = current;        current = next;    }    /*ADD A STATEMENT HERE*/}  What should be added in place of “/*ADD A STATEMENT HERE*/”, so that the function correctly reverses a linked list.

Which of the following methods is best for reversing a singly linked list in place (changing an existing list)?  *1 pointa) Using a stack to store the nodes and then rebuilding the listb) Using recursion to reverse the listc) Using an iterative approach with three pointers (prev, curr, next)d) Creating a new reversed list and copying the nodes from the original list

Write a C program to find reverse of a string using pointers

1/4

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.