
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program for 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
- Related Articles
- Reverse a Linked List using C++
- Program to reverse a linked list in Python
- Golang Program to reverse a given linked list.
- Reverse a Doubly Linked List using C++
- C Program to reverse each node value in Singly Linked List
- Reverse Linked List II in C++\n
- C program to display numbers in reverse order using single linked list
- Print reverse of a Linked List without extra space and modification in C Program.
- Program to reverse inner nodes of a linked list in python
- Print Immutable Linked List in Reverse in C++
- Reverse Linked List in Python
- Print Reverse a linked list using Stack
- Python Program to Reverse only First N Elements of a Linked List
- Reverse Alternate K Nodes in a Singly Linked List in C++
- How to Reverse a linked list in android?
