Reverse each word in a linked list node - GeeksforGeeks

21 Jun, 2021
Given a linked list of strings, we need to reverse each word of the string in the given linked list.Examples:  
Input: geeksforgeeks a computer science portal for geeks 
Output: skeegrofskeeg a retupmoc ecneics latrop rof skeeg

Input: Publish your own articles on geeksforgeeks
Output: hsilbuP ruoy nwo selcitra no skeegrofskeeg 
 
Using a loop iterate the list till null and take string from each node and reverse the string.  
C++
Java
Python3
C#
Javascript
// C++ program to reverse each word// in a linked list#include <bits/stdc++.h>using namespace std; // Linked list Node structurestruct Node {    string c;    struct Node* next;}; // Function to create newNode// in a linked liststruct Node* newNode(string c){    Node* temp = new Node;    temp->c = c;    temp->next = NULL;    return temp;}; // reverse each node datavoid reverse_word(string& str){    reverse(str.begin(), str.end());} void reverse(struct Node* head){    struct Node* ptr = head;     // iterate each node and call reverse_word    // for each node data    while (ptr != NULL) {        reverse_word(ptr->c);        ptr = ptr->next;    }} // printing linked listvoid printList(struct Node* head){    while (head != NULL) {        cout << head->c << " ";        head = head->next;    }} // Driver programint main(){    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");     cout << "List before reverse: \n";    printList(head);     reverse(head);     cout << "\n\nList after reverse: \n";    printList(head);     return 0;}
// Java program to reverse each word// in a linked listclass GFG{ // Linked list Node urestatic class Node{    String c;    Node next;}; // Function to create newNode// in a linked liststatic Node newNode(String c){    Node temp = new Node();    temp.c = c;    temp.next = null;    return temp;}; // reverse each node datastatic 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;     // iterate each node and call reverse_word    // for each node data    while (ptr != null)    {        ptr.c = reverse_word(ptr.c);        ptr = ptr.next;    }    return head;} // printing linked liststatic void printList( Node head){    while (head != null)    {        System.out.print( head.c + " ");        head = head.next;    }} // Driver programpublic 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: \n");    printList(head);     head = reverse(head);     System.out.print( "\n\nList after reverse: \n");    printList(head); }} // This code is contributed by Arnab Kundu
# Python3 program to reverse each word# in a linked list # Node of a linked listclass Node:    def __init__(self, next = None, data = None):        self.next = next        self.data = data # Function to create newNode# in a linked listdef newNode(c) :     temp = Node()    temp.c = c    temp.next = None    return temp # reverse each node datadef reverse_word(str) :    s = ""    i = 0    while(i < len(str) ):        s = str[i] + s        i = i + 1    return s def reverse( head) :    ptr = head     # iterate each node and call reverse_word    # for each node data    while (ptr != None):        ptr.c = reverse_word(ptr.c)        ptr = ptr.next         return head # printing linked listdef printList( head) :     while (head != None):        print( head.c ,end = " ")        head = head.next     # Driver programhead = 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") print( "List before reverse: ")printList(head) head = reverse(head) print( "\n\nList after reverse: ")printList(head) # This code is contributed by Arnab Kundu
// C# program to reverse each word// in a linked listusing System; class GFG{         // Linked list Node ure    public class Node    {        public String c;        public Node next;    };         // Function to create newNode    // in a linked list    static Node newNode(String c)    {        Node temp = new Node();        temp.c = c;        temp.next = null;        return temp;    }         // reverse each node data    static String reverse_word(String str)    {        String s = "";        for(int i = 0; i < str.Length; i++)            s = str[i] + s;        return s;    }         static Node reverse( Node head)    {        Node ptr = head;             // iterate each node and call reverse_word        // for each node data        while (ptr != null)        {            ptr.c = reverse_word(ptr.c);            ptr = ptr.next;        }        return head;    }         // printing linked list    static void printList( Node head)    {        while (head != null)        {            Console.Write( head.c + " ");            head = head.next;        }    }         // Driver program    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");             Console.Write( "List before reverse: \n");        printList(head);             head = reverse(head);             Console.Write( "\n\nList after reverse: \n");        printList(head);         }} // This code contributed by Rajput-Ji
<script> // JavaScript program to reverse each word// in a linked list // Linked list Node ureclass Node{    constructor()    {        this.c = "";        this.next = null;    }}; // Function to create newNode// in a linked listfunction newNode(c){    var temp = new Node();    temp.c = c;    temp.next = null;    return temp;} // reverse each node datafunction reverse_word(str){    var s = "";    for(var i = 0; i < str.length; i++)        s = str[i] + s;    return s;} function reverse(head){    var ptr = head;     // iterate each node and call reverse_word    // for each node data    while (ptr != null)    {        ptr.c = reverse_word(ptr.c);        ptr = ptr.next;    }    return head;} // printing linked listfunction printList( head){    while (head != null)    {        document.write( head.c + " ");        head = head.next;    }} // Driver programvar 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");document.write( "List before reverse: <br>");printList(head);head = reverse(head);document.write( "<br><br>List after reverse: <br>");printList(head); </script>
List before reverse: 
Geeksforgeeks a computer science portal for geeks 

List after reverse: 
skeegrofskeeG a retupmoc ecneics latrop rof skeeg
 
YouTubeGeeksforGeeks507K subscribersReverse each word in a linked list node | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:59•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ybT1uiWQYAs" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
andrew1234
Rajput-Ji
rrrtnx
Linked List
Strings
Linked List
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Linked List vs Array
Doubly Linked List | Set 1 (Introduction and Insertion)
Merge two sorted linked lists
Detect loop in a linked list
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4