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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Reverse a linked list
- Can we reverse a linked list in less than O(n)?
- Reverse even elements in a Linked List
- Reverse a Doubly Linked List | Set-2
- Reverse first K elements of given linked list
- Reverse a circular linked list
- Reverse a sublist of linked list
- Reverse a Doubly Linked List
- Reverse a Linked List according to its Size
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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