Reverse all the word in a String represented as a Linked List | Linked List Implementation ; Function to create a linked list from the given String ; Generates characters from the given string ; Function to reverse the words Assume str = " practice ▁ makes ▁ man ▁ perfect " to understand proper understanding of the code ; Initialize wordStartPosition to header ie . node ' p ' first letter of the word ' practice ' ; Navigate the linked list until a space ( ' ▁ ' ) is found or header is null ( this is for handing if there is only one word in the given sentence ) ; Keep track the previous node . This will become the last node of the linked list ; Keep moving to the next node ; If header is null then there is only one word in the given sentance so set header to the first / wordStartPosition node and return . ; store the node which is next to space ( ' ▁ ' ) ' m ' which is the first letter of the word ' make ' ; Setting sentenceStartPosition to node ' m ' ( first letter of the word ' make ' ) ; Again Navigate the linked list until a space ( ' ▁ ' ) is found or header == null end of the linked list ; When the next space is found , change the pointer to point the previous space ie . the word is being converted to " m - > a - > k - > e - > s - > ▁ - > p - > r - > a - > c - > t - > i - > c - > e " ; Newly found space will point to ' m ' first letter of the word ' make ' ; Repeat the loop until the end of the linked list is reached ; Set the last node 's next to null  ie. ->m->a->k->e->s-> ->p->r->a->->c->t->i->c->e->null ; Function to print the Linked List ; Navigate till the end and print the data in each node ; Driver code ; Convert given string to a linked list with character as data in each node
using System ; public class Node { public Node ( char data , Node next ) { Data = data ; Next = next ; } public char Data ; public Node Next ; } class GFG { private static Node CreateList ( String s ) { Node header = null ; Node temp = null ; for ( int i = 0 ; i < s . Length ; i ++ ) { char c = s [ i ] ; Node node = new Node ( c , null ) ; if ( header == null ) { header = node ; temp = header ; } else { temp . Next = node ; temp = temp . Next ; } } return header ; } private static Node Reverse ( Node header ) { if ( header == null ) return header ; Node wordStartPosition = null ; Node endOfSentence = null ; Node sentenceStartPosition = null ; wordStartPosition = header ; while ( header != null && header . Data != ' ▁ ' ) { endOfSentence = header ; header = header . Next ; } if ( header == null ) { header = wordStartPosition ; return header ; } do { Node temp = header . Next ; header . Next = wordStartPosition ; wordStartPosition = header ; header = temp ; Node prev = null ; sentenceStartPosition = header ; while ( header != null && header . Data != ' ▁ ' ) { prev = header ; header = header . Next ; } prev . Next = wordStartPosition ; wordStartPosition = sentenceStartPosition ; if ( header == null ) break ; } while ( header != null ) ; header = sentenceStartPosition ; endOfSentence . Next = null ; return header ; } private static void PrintList ( Node Header ) { Node temp = Header ; while ( temp != null ) { Console . Write ( temp . Data ) ; temp = temp . Next ; } } public static void Main ( String [ ] args ) { String s = " practice ▁ makes ▁ a ▁ man ▁ perfect " ; Node header = CreateList ( s ) ; Console . WriteLine ( " Before : " ) ; PrintList ( header ) ; header = Reverse ( header ) ; Console . WriteLine ( " After : " PrintList ( header ) ; } }