Reverse each word in a linked list node | Java program to reverse each word in a linked list ; Linked list Node ure ; Function to create newNode in a linked list ; reverse each node data ; iterate each node and call reverse_word for each node data ; printing linked list ; Driver program
class GFG { static class Node { String c ; Node next ; } ; static Node newNode ( String c ) { Node temp = new Node ( ) ; temp . c = c ; temp . next = null ; return temp ; } ; static String reverse_word ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) s = str . charAt ( i ) + s ; return s ; } static Node reverse ( Node head ) { Node ptr = head ; while ( ptr != null ) { ptr . c = reverse_word ( ptr . c ) ; ptr = ptr . next ; } return head ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . c + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = newNode ( " Geeksforgeeks " ) ; head . next = newNode ( " a " ) ; head . next . next = newNode ( " computer " ) ; head . next . next . next = newNode ( " science " ) ; head . next . next . next . next = newNode ( " portal " ) ; head . next . next . next . next . next = newNode ( " for " ) ; head . next . next . next . next . next . next = newNode ( " geeks " ) ; System . out . print ( "List before reverse: NEW_LINE"); printList ( head ) ; head = reverse ( head ) ; System . out . print ( " List after reverse : "); printList ( head ) ; } }