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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reverse each word in a linked list node
- Reverse every word of the string except the first and the last character
- First substring whose reverse is a word in the string
- 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 a circular linked list
- Reverse even elements in a Linked List
- Reverse first K elements of given linked list
- Reverse a Linked List according to its Size
- Reverse a sublist of linked list
- Reverse a Doubly Linked List
- Reverse a Doubly Linked List | Set-2
- Reverse a Linked List in groups of given size | Set 2
- Java Program for Reverse a linked list
- Print reverse of a Linked List without actually reversing
- Program to reverse a linked list using Stack
- Print Reverse a linked list using Stack
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