Create a singly linked list of n nodes and display it in reverse order C

C Exercises: Create a singly linked list and print it in reverse order

Last update on December 20 2021 09:10:21 (UTC/GMT +8 hours)

Reverse a linked list

Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes.

Examples:

Input: Head of following linked list
1->2->3->4->NULL
Output: Linked list should be changed to,
4->3->2->1->NULL

Input: Head of following linked list
1->2->3->4->5->NULL
Output: Linked list should be changed to,
5->4->3->2->1->NULL

Input: NULL
Output: NULL



Input: 1->NULL
Output: 1->NULL

Required knowledge

Basic C programming, Functions, Singly Linked List, Dynamic memory allocation

Algorithm to reverse a Singly Linked List

Algorithm to reverse a Singly Linked List %%Input : head node of the linked list Begin: If (head != NULL) then prevNode ← head head ← head.next curNode ← head prevNode.next ← NULL While (head != NULL) do head ← head.next curNode.next ← prevNode prevNode ← curNode curNode ← head End while head ← prevNode End if End

C program to display numbers in reverse order using single linked list

CServer Side ProgrammingProgramming

Linked lists use dynamic memory allocation and are collection of nodes.

Nodes have two parts which are data and link.

C program to display a Linked List in Reverse

Display a linked list in reverse: Here, we are going to implement a C program to display the linked list in reverse using recursion.
Submitted by Radib Kar, on December 25, 2018

Problem statement: Write a program to display the linked list in reverse order. Note that the original linked list will not change.

Solution

  1. Create and build the linked list
  2. Display the original linked list
  3. Display in reverse order

Displaying in reverse order can be done using recursive function.

Function reverse_display(node) IF (node!= NULL) reverse_display(node->next) display node value END IF

Example with Explanation:

Let's check how the program runs...

Let the input linked list to be: 1->2->3->4->NULL with head at 1

In Main() it calls reverse_display(1) ---------------------------------------------------------------- reverse_display(1): node is not null reverse_display(1->next) thus it calls reverse_display(2) ---------------------------------------------------------------- reverse_display(2): node is not null reverse_display(2->next) thus it calls reverse_display(3) ---------------------------------------------------------------- reverse_display(3): node is not null reverse_display(3->next) thus it calls reverse_display(4) ---------------------------------------------------------------- reverse_display(4): node is not null reverse_display(4->next) thus it calls reverse_display(NULL) ---------------------------------------------------------------- reverse_display(NULL): node is null no further call, control returned to reverse_display(4) ---------------------------------------------------------------- At reverse_display(4) Control returned from reverse_display(4->next) So it prints the node value that is 4 Control returns to reverse_display(3) ---------------------------------------------------------------- At reverse_display(3) Control returned from reverse_display(3->next) So it prints the node value that is 3 Control returns to reverse_display(2) ---------------------------------------------------------------- At reverse_display(2) Control returned from reverse_display(2->next) So it prints the node value that is 2 Control returns to reverse_display(1) ---------------------------------------------------------------- At reverse_display(1) Control returned from reverse_display(1->next) So it prints the node value that is 1 Control returns to Main function Thus it displays 4 3 2 1
ADVERTISEMENT