1/**
2 * C program to reverse a Singly Linked List
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8
9/* Structure of a node */
10struct node {
11 int data; //Data part
12 struct node *next; //Address part
13}*head;
14
15
16/* Functions used in the program */
17void createList(int n);
18void reverseList();
19void displayList();
20
21
22int main()
23{
24 int n, choice;
25
26 /*
27 * Create a singly linked list of n nodes
28 */
29 printf("Enter the total number of nodes: ");
30 scanf("%d", &n);
31 createList(n);
32
33 printf("\nData in the list \n");
34 displayList();
35
36 /*
37 * Reverse the list
38 */
39 printf("\nPress 1 to reverse the order of singly linked list\n");
40 scanf("%d", &choice);
41 if(choice == 1)
42 {
43 reverseList();
44 }
45
46 printf("\nData in the list\n");
47 displayList();
48
49 return 0;
50}
51
52
53/*
54 * Create a list of n nodes
55 */
56void createList(int n)
57{
58 struct node *newNode, *temp;
59 int data, i;
60
61 if(n <= 0)
62 {
63 printf("List size must be greater than zero.\n");
64 return;
65 }
66
67 head = (struct node *)malloc(sizeof(struct node));
68
69 /*
70 * If unable to allocate memory for head node
71 */
72 if(head == NULL)
73 {
74 printf("Unable to allocate memory.");
75 }
76 else
77 {
78 /*
79 * Read data of node from the user
80 */
81 printf("Enter the data of node 1: ");
82 scanf("%d", &data);
83
84 head->data = data; // Link the data field with data
85 head->next = NULL; // Link the address field to NULL
86
87 temp = head;
88
89 /*
90 * Create n nodes and adds to linked list
91 */
92 for(i=2; i<=n; i++)
93 {
94 newNode = (struct node *)malloc(sizeof(struct node));
95
96 /* If memory is not allocated for newNode */
97 if(newNode == NULL)
98 {
99 printf("Unable to allocate memory.");
100 break;
101 }
102 else
103 {
104 printf("Enter the data of node %d: ", i);
105 scanf("%d", &data);
106
107 newNode->data = data; // Link the data field of newNode with data
108 newNode->next = NULL; // Link the address field of newNode with NULL
109
110 temp->next = newNode; // Link previous node i.e. temp to the newNode
111 temp = temp->next;
112 }
113 }
114
115 printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
116 }
117}
118
119
120/*
121 * Reverse the order of nodes of a singly linked list
122 */
123void reverseList()
124{
125 struct node *prevNode, *curNode;
126
127 if(head != NULL)
128 {
129 prevNode = head;
130 curNode = head->next;
131 head = head->next;
132
133 prevNode->next = NULL; // Make first node as last node
134
135 while(head != NULL)
136 {
137 head = head->next;
138 curNode->next = prevNode;
139
140 prevNode = curNode;
141 curNode = head;
142 }
143
144 head = prevNode; // Make last node as head
145
146 printf("SUCCESSFULLY REVERSED LIST\n");
147 }
148}
149
150
151/*
152 * Display entire list
153 */
154void displayList()
155{
156 struct node *temp;
157
158 /*
159 * If the list is empty i.e. head = NULL
160 */
161 if(head == NULL)
162 {
163 printf("List is empty.");
164 }
165 else
166 {
167 temp = head;
168 while(temp != NULL)
169 {
170 printf("Data = %d\n", temp->data); // Print the data of current node
171 temp = temp->next; // Move to next node
172 }
173 }
174}
1#include <stdio.h>
2struct Node {
3 int data;
4 struct Node* next;
5 Node(int data){
6 this->data = data;
7 next = NULL;
8 }
9};
10struct LinkedList {
11 Node* head;
12 LinkedList(){
13 head = NULL;
14 }
15 void interReverseLL(){
16 Node* current = head;
17 Node *prev = NULL, *after = NULL;
18 while (current != NULL) {
19 after = current->next;
20 current->next = prev;
21 prev = current;
22 current = after;
23 }
24 head = prev;
25 }
26 void print() {
27 struct Node* temp = head;
28 while (temp != NULL) {
29 printf("%d ", temp-> data);
30 temp = temp->next;
31 }
32 printf("\n");
33 }
34 void push(int data){
35 Node* temp = new Node(data);
36 temp->next = head;
37 head = temp;
38 }
39};
40int main() {
41 LinkedList linkedlist;
42 linkedlist.push(85);
43 linkedlist.push(10);
44 linkedlist.push(65);
45 linkedlist.push(32);
46 linkedlist.push(9);
47 printf("Linked List : \t");
48 linkedlist.print();
49 linkedlist.interReverseLL();
50 printf("Reverse Linked List : \t");
51 linkedlist.print();
52 return 0;
53}
54
55
56Output
57Linked List : 9 32 65 10 85
58Reverse Linked List : 85 10 65 32 9