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⏎
Question
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⏎
Solution
Here is a C program that reverses a singly linked list recursively:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
struct Node* reverseList(struct Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct Node* rest = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
int main() {
struct Node* head = newNode(5);
head->next = newNode(4);
head->next->next = newNode(3);
head->next->next->next = newNode(2);
head->next->next->next->next = newNode(1);
printf("Original linked list: ");
printList(head);
head = reverseList(head);
printf("Reversed linked list: ");
printList(head);
return 0;
}
This program first creates a linked list with nodes 5 -> 4 -> 3 -> 2 -> 1 -> NULL. It then calls the reverseList function to reverse the linked list. The reverseList function works by recursively reversing the rest of the list and then making the next node of the current node point to the current node itself. Finally, it prints out the reversed linked list.
Similar Questions
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
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
reverse a linked list- DATA STRUCTURESWrite a program to reverse a linked list.Constraints:Integers onlyExample:Input FormatTo get the number of elements followed by the elements separated by a single space.Output FormatDisplays the reversed list.Note: There is a space at the end of the outputExplanation:linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 3 67 45 1212 45 672 5 -1 3 2 6 44 6 2 3 -1
How can you efficiently reverse the order of the elements in a singly linked list? Iterate through the list and swap elements in pairs. Create a new list and add elements in reverse order. Use recursion to recursively reverse sub-lists. Reverse the pointers of each node in the list.
Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.Input FormatFirst line of input contains n- the size of first linked list. Next line of input contains n integers the n elements of the first linked list.Constraints1 <= N <= 10^41 <= k <= NOutput FormatPrint the modified linked list.Sample input81 2 2 4 5 6 7 84Sample output4 2 2 1 8 7 6 5
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.