# Reverse each word in a linked list Node

A linked list is a chain alike linear data structure, where the elements are not saved at memory in an adjacent manner, unlike array. In a particular linked list, the elements are linked with the next element by a pointer. In simple words; a linked list is a series of some data containers where we can find a path way or a reference link in those elements to the next node. There is a head pointer present as the first element in a linked list. If the first node of that particular list is empty then it points nothing or void.

There are different types of linked list present in data structure.

• Singly Linked list − It is a basic type of linked list present in data structure, where every node contains some data with a pointer of the same data type for the next node. Here for this linked list both the time complexity and auxiliary space is O(n).

• Doubly Linked List − It is a complex two way linked list that contains a pointer as a sequence in the previous node. This type of linked list contains with three different parts of data source, a pointer and the next node. With this we can traverse the whole list in a backward direction.

• Circular Linked List − In a circular linked list the first node pointer indicated by the last node of the list. It means, the list has no start and no ending point. To traverse a circular linked list, the user can start from any node and traverse the list in forward or backward direction as their wish.

• Doubly Circular linked list − This is a two way circular linked list that contains a pointer of both previous and next node. It does not contains null for the previous first node.

Here in this article, we will build some code for those mentioned linked list by which we will learn to reverse each word in a linked list node in a C++ environment.

## Algorithm to reverse each word present in a linked list node

• Step 1 − Declare a temporary array.

• Step 2 − Traverse a linked list.

• Step 3 − If the current element is an alphabet then store the element.

• Step 4 − Else, increase node by 1 pointer.

• Step 5 − Again traverse from the head.

• Step 6 − If the current element is alphabet then copy it to the last element.

• Step 7 − Decrease the current index.

• Step 8 − Iteration has to be made.

• Step 9 − Else, increase it by one.

### Syntax to reverse each word present in a linked list node

insertEnd(head, new_node)
Declare last
new_node->next = new_node->prev = new_node
return
new_node->prev = last
last->next = new_node
Declare last
Initialize curr = last, prev
while curr->prev != last
prev = curr->prev
curr = prev


### Approaches to follow:-

• Approach 1 − Reverse each word present in a linked list

• Approach 2 − Reverse the whole sentence present in a linked list.

• Approach 3 − Reverse a doubly circular linked list.

• Approach 4 − Reverse a circular linked list.

• Approach 5 − Reverse a linked list without affecting special characters.

## Reverse each word present in a linked list by using C++

Here in this particular C++ build code we have reversed each word present in a linked list.

### Example 1

#include <bits/stdc++.h>
using namespace std;
struct Node {
string c;
struct Node* next;
};
struct Node* newNode(string c){
Node* temp = new Node;
temp->c = c;
temp->next = NULL;
return temp;
};
void reverse_word(string& str){
reverse(str.begin(), str.end());
}
while (ptr != NULL) {
reverse_word(ptr->c);
ptr = ptr->next;
}
}
cout << head->c << " ";
}
}
int main(){
Node* head = newNode("Train number 13109");
head->next->next = newNode("is an international train");
cout << "The list here present before reverse: \n";
cout << "\n\nList after reverse we can see like: \n";
return 0;
}


### Output

The list here present before reverse:
Train number 13109 Maitree Express is an international train runs between Kolkata and Dhaka

List after reverse we can see like:
90131 rebmun niarT sserpxE eertiaM niart lanoitanretni na si neewteb snur atakloK dna akahD


## Reverse the whole sentence present in a linked list

Here in this particular code we have reversed the whole sentence present in a linked list.

#include <bits/stdc++.h>
using namespace std;
string reverseString(string str){
reverse(str.begin(), str.end());
str.insert(str.end(), ' ');
int n = str.length();
int j = 0;
for (int i = 0; i < n; i++) {
if (str[i] == ' ') {
reverse(str.begin() + j,
str.begin() + i);
j = i + 1;
}
}
str.pop_back();
return str;
}
int main(){
string str = "13110, Maitree Express Is An International Train Runs Between Dhaka And Kolkata";
string rev = reverseString(str);
cout << rev;
return 0;
}


### Output

Kolkata And Dhaka Between Runs Train International An Is Express Maitree 13110,


## Reverse a doubly circular linked list

Here in this particular code we have reversed a doubly circular linked list.

### Example 3

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *next, *prev;
};
Node* getNode(int data){
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
return newNode;
}
void insertEnd(Node** head, Node* new_node) {
new_node->next = new_node->prev = new_node;
return;
}
new_node->prev = last;
last->next = new_node;
}
return NULL;
Node *curr = last, *prev;
while (curr->prev != last) {
prev = curr->prev;
curr = prev;
}
}
return;
cout << "Forward direction data source: ";
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data;
temp = last;
cout << "\nBackward direction data source: ";
while (temp->prev != last) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << temp->data;
}
int main(){
cout << "Current list here present:\n";
cout << "\n\nReversed list here present:\n";
return 0;
}


### Output

Current list here present:
Forward direction data source: 16 10 7 2001 1997
Backward direction data source: 1997 2001 7 10 16

Reversed list here present:
Forward direction data source: 1997 2001 7 10 16
Backward direction data source: 16 10 7 2001 1997


## Reverse a circular linked list

Here in this particular code we have reversed the data set of a circular linked list.

### Example 4

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode(int data){
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
return;
Node* prev = NULL;
Node* next;
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return;
do {
cout << temp->data << " ";
temp = temp->next;
}
int main(){
cout << "Given circular linked list is here: ";
cout << "\nReversed circular linked list after method: ";
return 0;
}


### Output

Given circular linked list is here: 10 16 7 2022
Reversed circular linked list after method: 2022 7 16 10


## Reverse a linked list without affecting special characters

Here in this particular code we have reversed the data set of linked list without affecting special characters.

### Example 5

#include <iostream>
using namespace std;
struct Node {
char data;
struct Node* next;
};
void reverse(struct Node** head_ref, int size){
char TEMP_ARR[size];
int i = 0;
while (current != NULL) {
if ((current->data >= 97 && current->data <= 122) ||
(current->data >= 65 && current->data <= 90)) {
TEMP_ARR[i++] = current->data;
current = current->next;
}
else
current = current->next;
}
while (current != NULL) {
if ((current->data >= 97 && current->data <= 122) ||
(current->data >= 65 && current->data <= 90)) {
current->data = TEMP_ARR[--i];
current = current->next;
}
else
current = current->next;
}
}
void push(struct Node** head_ref, char new_data){
struct Node* new_node = new Node();
new_node->data = new_data;
}
while (temp != NULL) {
cout << temp->data;
temp = temp->next;
}
}
// Driver program to test above function
int main() {
cout << "Given linked list is here: ";
cout << "\nReversed Linked list is here: ";
return 0;
}


### Output

Given linked list is here: B@010ATAKLOKARDUR
Reversed Linked list is here: R@010UDRAKOLKATAB


## Conclusion

In this article today, we have learnt how to reverse each word present in a linked list node. We have built here the C++ code with the possible methods to give you a broad view about the reversing processes possible with a linked list node.

Updated on: 05-Apr-2023

108 Views 