Given a Linked List which represents a sentence S such that each node represents a letter, the task is to reverse the sentence without reversing individual words.
For example, for a given sentence “I love Geeks for Geeks”, the Linked List representation is given as:
I-> ->l->o->v->e-> ->G->e->e->k->s-> ->f->o->r-> ->G->e->e->k->s
Input: I love Geeks for Geeks
Output: Geeks for Geeks love I
Input: practice makes a man perfect
Output: perfect man a makes practice
Approach: The idea is to navigate the linked list from the beginning. Every time space is encountered, swap the space to the beginning of that word. Repeat this step until the last node is reached. Finally set the first words last letter to point null which will become the last node and keep changing the pointers.
Below is the implementation of the approach:
Before: practice makes a man perfect After: perfect man a makes practice
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Reverse each word in a linked list node
- First substring whose reverse is a word in the string
- Reverse every word of the string except the first and the last character
- Add one to a number represented as linked list | Set 2
- Add 1 to a number represented as linked list
- Multiply two numbers represented as linked lists into a third list
- Can we reverse a linked list in less than O(n)?
- Reverse a linked list
- Reverse first K elements of given linked list
- Reverse a Doubly Linked List | Set-2
- Reverse a sublist of linked list
- Reverse a circular linked list
- Reverse a Linked List according to its Size
- Reverse even elements in a Linked List
- Reverse a Doubly Linked List
- Reverse a Doubly linked list using recursion
- Program to reverse a linked list using Stack
- Print Reverse a linked list using Stack
- Java Program for Reverse a linked list
- Reverse a Linked List in groups of given size | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rajput-Ji