
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to Reverse a Linked List
In this problem, we are given a linked list. Our task is to create a program for reverse a linked list.
The program will reverse the given linked list and return the reversed linked list.
Linked List is a sequence of links with contains items. Each link contains a connection to another link.
Example
9 -> 32 -> 65 -> 10 -> 85 -> NULL
Reverse linked list is a linked list created to form a linked list by reversing the links of the list. The head node of the linked list will be the last node of the linked list and the last one will be the head node.
Example
Reverse linked list formed from the above linked list −
85 -> 10 -> 65 -> 32 -> 9 -> NULL
To reverse the given linked list we will use three extra pointers that will be in the process. The pointers will be previous, after, current.
We will initialize previous and after to NULL initially and current to head of the linked list.
After this, we will iterate until we reach the NULL of the initial (non-reversed linked list). And do the following −
after = current -> next current -> next = previous previous = current current = after
Now let’s create a program for reversing the linked list. There can be two ways to create the program, one is iterative and the other one is recursive.
Program for Reverse a linked list (tail-recursive approach)
Example
#include <stdio.h> 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("
"); } 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("Linked list : \t"); printLinkedList(head1); tailRecReveseLL(&head1); printf("Reversed linked list : \t"); printLinkedList(head1); return 0; }
Output
Linked list : 9 32 65 10 85 Reversed linked list : 85 10 65 32 9
Program for Reverse a linked list (iterative approach)
Example
#include <stdio.h> struct Node { int data; struct Node* next; Node(int data){ this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList(){ head = NULL; } void interReverseLL(){ Node* current = head; Node *prev = NULL, *after = NULL; while (current != NULL) { after = current->next; current->next = prev; prev = current; current = after; } head = prev; } void print() { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp-> data); temp = temp->next; } printf("
"); } void push(int data){ Node* temp = new Node(data); temp->next = head; head = temp; } }; int main() { LinkedList linkedlist; linkedlist.push(85); linkedlist.push(10); linkedlist.push(65); linkedlist.push(32); linkedlist.push(9); printf("Linked List : \t"); linkedlist.print(); linkedlist.interReverseLL(); printf("Reverse Linked List : \t"); linkedlist.print(); return 0; }
Output
Linked List : 9 32 65 10 85 Reverse Linked List : 85 10 65 32 9