
- 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
Print reverse of a Linked List without extra space and modification in C Program.
The task is to print the nodes starting from the end of the linked list without using extra space which means there shouldn’t be any extra variable instead the head pointer pointing the first node will be moved.
Example
Input: 10 21 33 42 89 Output: 89 42 33 21 10
There can be many solutions to print the linked list in reverse order, like recursive approach (use extra space), reverse the linked list(requires modification in the given Linked List), pushing the elements on a stack and then pop and display the elements one by one(requires space O(n)), but these solutions seem to be using more space than the O(1).
To achieve the result without using more than O(1) we can −
- Count the number of nodes in a linked list
- Loop from i = n to 1 and print the i-th location’s node.
Algorithm
START Step 1 -> create node variable of type structure Declare int data Declare pointer of type node using *next Step 2 ->Declare function int get(struct node* head) Declare variable as int count=0 Declare struct node *newme=head Loop While newme!=NULL Increment count by 1 Set newme = newme->next End Return count Step 3 -> Declare Function void push(node** headref, char newdata) Allocate memory using malloc Set newnode->data = newdata Set newnode->next = (*headref) Set (*headref) = newnode Step 4 -> Declare function int getN(struct node* head, int n) Declare struct node* cur = head Loop for int i=0 and i<n-1 && cur != NULL and i++ Set cur=cur->next End Return cur->dataStep 5 -> Declare function void reverse(node *head) Declare int n = get(head) Loop For int i=n and i>=1 and i— Print getN(head,i) End Step 6 ->In Main() Create list using node* head = NULL Insert elements through push(&head, 89) Call reverse(head) STOP
Example
#include<stdio.h> #include<stdlib.h> //node structure struct node { int data; struct node* next; }; void push(struct node** headref, int newdata) { struct node* newnode = (struct node*) malloc(sizeof(struct node)); newnode->data = newdata; newnode->next = (*headref); (*headref) = newnode; } int get(struct node* head) { int count = 0; struct node* newme = head; while (newme != NULL){ count++; newme = newme->next; } return count; } int getN(struct node* head, int n) { struct node* cur = head; for (int i=0; i<n-1 && cur != NULL; i++) cur = cur->next; return cur->data; } void reverse(node *head) { int n = get(head); for (int i=n; i>=1; i--) printf("%d ", getN(head, i)); } int main() { struct node* head = NULL; //create a first node push(&head, 89); //pushing element in the list push(&head, 42); push(&head, 33); push(&head, 21); push(&head, 10); reverse(head); //calling reverse function return 0; }
Output
if we run above program then it will generate following output
89 42 33 21 10
- Related Articles
- Print reverse of a Linked List without actually reversing in C language
- Print Immutable Linked List in Reverse in C++
- Find pair for given sum in a sorted singly linked without extra space in C++
- C Program for Reverse a linked list
- Print Reverse a linked list using Stack
- JavaScript Program for Printing Reverse of a Linked List Without Actually Reversing
- An interesting method to print reverse of a linked list in C++
- Java Program To Reverse A Linked List Without Manipulating Its Pointers
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Program to reverse a linked list in Python
- Reverse a Linked List using C++
- C++ program to replace all occurrences of string AB with C without using extra space
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Print n x n spiral matrix using O(1) extra space in C Program.
- Reverse a Doubly Linked List using C++

Advertisements