1class LinkedList {
2
3 static Node head;
4
5 static class Node {
6
7 int data;
8 Node next;
9
10 Node(int d)
11 {
12 data = d;
13 next = null;
14 }
15 }
16
17 /* Function to reverse the linked list */
18 Node reverse(Node node)
19 {
20 Node prev = null;
21 Node current = node;
22 Node next = null;
23 while (current != null) {
24 next = current.next;
25 current.next = prev;
26 prev = current;
27 current = next;
28 }
29 node = prev;
30 return node;
31 }
32
33 // prints content of double linked list
34 void printList(Node node)
35 {
36 while (node != null) {
37 System.out.print(node.data + " ");
38 node = node.next;
39 }
40 }
41
42 public static void main(String[] args)
43 {
44 LinkedList list = new LinkedList();
45 list.head = new Node(85);
46 list.head.next = new Node(15);
47 list.head.next.next = new Node(4);
48 list.head.next.next.next = new Node(20);
49
50 System.out.println("Given Linked list");
51 list.printList(head);
52 head = list.reverse(head);
53 System.out.println("");
54 System.out.println("Reversed linked list ");
55 list.printList(head);
56 }
57}
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}
1class recursion {
2 static Node head; // head of list
3 static class Node {
4 int data;
5 Node next;
6 Node(int d)
7 { data = d;
8 next = null; } }
9 static Node reverse(Node head)
10 {
11 if (head == null || head.next == null)
12 return head;
13 /* reverse the rest list and put the first element
14 at the end */
15 Node rest = reverse(head.next);
16 head.next.next = head;
17 /* tricky step -- see the diagram */
18 head.next = null;
19 /* fix the head pointer */
20 return rest;
21 } /* Function to print linked list */
22 static void print()
23 {
24 Node temp = head;
25 while (temp != null) {
26 System.out.print(temp.data + " ");
27 temp = temp.next;
28 }
29 System.out.println();
30 }
31 static void push(int data)
32 {
33 Node temp = new Node(data);
34 temp.next = head;
35 head = temp;
36 } /* Driver program to test above function*/
37public static void main(String args[])
38{
39 /* Start with the empty list */
40 push(20);
41 push(4);
42 push(15);
43 push(85);
44 System.out.println("Given linked list");
45 print();
46 head = reverse(head);
47 System.out.println("Reversed Linked list");
48 print();
49} } // This code is contributed by Prakhar Agarwal
1 /* Before changing next pointer of current node,
2 store the next node */
3 next = curr -> next
4 /* Change next pointer of current node */
5 /* Actual reversing */
6 curr -> next = prev
7 /* Move prev and curr one step ahead */
8 prev = curr
9 curr = next
10
1#include <stdio.h>
2#include <stdlib.h>
3
4struct node
5{
6 int num; //Data of the node
7 struct node *nextptr; //Address of the node
8}*stnode;
9
10void createNodeList(int n); //function to create the list
11void reverseDispList(); //function to convert the list in reverse
12void displayList(); //function to display the list
13
14int main()
15{
16 int n;
17 printf("\n\n Linked List : Create a singly linked list and print it in reverse order :\n");
18 printf("------------------------------------------------------------------------------\n");
19
20 printf(" Input the number of nodes : ");
21 scanf("%d", &n);
22 createNodeList(n);
23 printf("\n Data entered in the list are : \n");
24 displayList();
25 reverseDispList();
26 printf("\n The list in reverse are : \n");
27 displayList();
28 return 0;
29}
30
31void createNodeList(int n)
32{
33 struct node *fnNode, *tmp;
34 int num, i;
35 stnode = (struct node *)malloc(sizeof(struct node));
36 if(stnode == NULL) //check whether the stnode is NULL and if so no memory allocation
37 {
38 printf(" Memory can not be allocated.");
39 }
40 else
41 {
42// reads data for the node through keyboard
43 printf(" Input data for node 1 : ");
44 scanf("%d", &num);
45 stnode-> num = num;
46 stnode-> nextptr = NULL; //Links the address field to NULL
47 tmp = stnode;
48//Creates n nodes and adds to linked list
49 for(i=2; i<=n; i++)
50 {
51 fnNode = (struct node *)malloc(sizeof(struct node));
52 if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory allocation
53 {
54 printf(" Memory can not be allocated.");
55 break;
56 }
57 else
58 {
59 printf(" Input data for node %d : ", i);
60 scanf(" %d", &num);
61 fnNode->num = num; // links the num field of fnNode with num
62 fnNode->nextptr = NULL; // links the address field of fnNode with NULL
63 tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
64 tmp = tmp->nextptr;
65 }
66 }
67 }
68}
69
70void reverseDispList()
71{
72 struct node *prevNode, *curNode;
73
74 if(stnode != NULL)
75 {
76 prevNode = stnode;
77 curNode = stnode->nextptr;
78 stnode = stnode->nextptr;
79
80 prevNode->nextptr = NULL; //convert the first node as last
81
82 while(stnode != NULL)
83 {
84 stnode = stnode->nextptr;
85 curNode->nextptr = prevNode;
86
87 prevNode = curNode;
88 curNode = stnode;
89 }
90 stnode = prevNode; //convert the last node as head
91 }
92}
93
94void displayList()
95{
96 struct node *tmp;
97 if(stnode == NULL)
98 {
99 printf(" No data found in the list.");
100 }
101 else
102 {
103 tmp = stnode;
104 while(tmp != NULL)
105 {
106 printf(" Data = %d\n", tmp->num); // prints the data of current node
107 tmp = tmp->nextptr; // advances the position of current node
108 }
109 }
110}
111
112
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