Invierta todas las palabras en una string representada como una lista enlazada

Dada una lista enlazada que representa una oración S tal que cada Node representa una letra, la tarea es invertir la oración sin invertir las palabras individuales. 
Por ejemplo, para una oración dada «Me encantan los geeks para geeks», la representación de la lista enlazada se da como: 
I-> ->l->o->v->e-> ->G->e->e- >k->s-> ->f->o->r-> ->G->e->e->k->s 

Ejemplos: 

Entrada: Me encanta Geeks for Geeks 
Salida: Geeks for Geeks me encanta

Entrada: la práctica hace que un hombre sea perfecto 
Salida: un hombre perfecto hace que la práctica 

Enfoque: La idea es navegar por la lista enlazada desde el principio. Cada vez que encuentre un espacio, cambie el espacio al comienzo de esa palabra. Repita este paso hasta llegar al último Node. Finalmente, establezca la última letra de las primeras palabras en el punto nulo, que se convertirá en el último Node y siga cambiando los punteros.

A continuación se muestra la implementación del enfoque:  

Java

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

C#

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

Before:
practice makes a man perfect
After:
perfect man a makes practice

 

Publicación traducida automáticamente

Artículo escrito por bdineshbabutsm y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *