Posted On: Dec 24, 2020
Link List is a sequence of links that contain items in which each item contains a connection to another item.
A reverse link list is a linked list created to form a linked list by reversing the items of the list. The head node of the linked list will become the last node of the linked list and the last one will be the head node.
Program to Reverse the order of the given link list:
#include struct Node { int data; struct Node* next; }; Node* insertNode(int key) { Node* temp = new Node; temp->data = key; temp->next = NULL; return temp; } void tailRecRevese(Node* current, Node* previous, Node** head){ if (!current->next) { *head = current; current->next = previous; return; } Node* next = current->next; current->next = previous; tailRecRevese(next, current, head); } void tailRecReveseLL(Node** head){ if (!head) return; tailRecRevese(*head, NULL, head); } void printLinkedList(Node* head){ while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("n"); } int main(){ Node* head1 = insertNode(9); head1->next = insertNode(32); head1->next->next = insertNode(65); head1->next->next->next = insertNode(10); head1->next->next->next->next = insertNode(85); printf("Link list: t"); printLinkedList(head1); tailRecReveseLL(&head1); printf("The output is as follows: t"); printLinkedList(head1); return 0; }
Never Miss an Articles from us.
The STP also stands for Spanning Tree Protocol constructs a loop-free logical topography for Ethernet networks. It is a ..
A prime number is a whole number greater than 1, which is only divisible by 1 and itself. // function check whether a number // is prime or not bool isPrime(int n) { // Corner case if (n...
A regular expression to validate phone number in XXX-XXX-XXXX format is follows: /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/...