Reverse nodes of a linked list without affecting the special characters

Given a linked list of alphabets and special characters. Reverse the given linked list without affecting the position of the special characters.

Examples:

Input: g -> @ -> e -> # -> e -> $ -> k -> s -> NULL
Output: s -> @ -> k -> # -> e -> $ -> e -> g -> NULL
Explanation: Here we can see that in the output the position of special character in not change and also linked list is reverse.



The idea is to traverse the linked list and store the characters excluding the special characters in a temporary array. Again traverse the linked list and copy elements from the array to the nodes of the linked list in a reverse manner.

Below is the step by step algorithm:

  1. Take a temporary array, TEMP_ARR.
  2. Traverse the linked list and do the following
    • if the current element is an alphabet, store that element of the linked list to TEMP_ARR.
    • else, increase node pointer by one
    • Again traverse the linked list from the head and TEMP_ARR from the end and do the following:

    • if the current element is an alphabet, copy the last element of TEMP_ARR to the current linked list node and decrease the current index of TEMP_ARR for the next iteration.
    • else, increase node by one

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to reverse a linked list
// without affecting special characters
  
#include <iostream>
  
using namespace std;
  
// Link list node 
struct Node {
    char data;
    struct Node* next;
};
  
// Function to reverse the linked list
// without affecting special characters
void reverse(struct Node** head_ref, int size)
{
    struct Node* current = *head_ref;
      
      
    char TEMP_ARR[size];
      
    int i = 0;
      
    // Traverse the linked list and insert
    // linked list elements to TEMP_ARR
    while (current != NULL) {
        // if the cuurent data is any alphabet than
        // store it in to TEMP_ARR
        if ((current->data >= 97 && current->data <= 122) || 
                (current->data >= 65 && current->data <= 90)) {
            TEMP_ARR[i++] = current->data;
            current = current->next;
        }
        // else increase the node position
        else
            current = current->next;
    }
      
    current = *head_ref;
    // Traverse the linked list again
    while (current != NULL) 
    {
        // if current character is an alphabet than
        // replace the current element in the linked list
        // with the last element of the TEMP_ARR
        if ((current->data >= 97 && current->data <= 122) || 
                (current->data >= 65 && current->data <= 90)) {
            current->data = TEMP_ARR[--i];
            current = current->next;
        }
        // else increase the node
        else
            current = current->next;
    }
}
  
// Function to push a node 
void push(struct Node** head_ref, char new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data */
    new_node->data = new_data;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
/* Function to print linked list */
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        cout << temp->data;
        temp = temp->next;
    }
}
  
// Driver program to test above function
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
  
    push(&head, 's');
    push(&head, '$');
    push(&head, 'k');
    push(&head, 'e');
    push(&head, 'e');
    push(&head, '@');
    push(&head, '#');
    push(&head, 'g');
    push(&head, 'r');
    push(&head, 'o');
    push(&head, 'f');
    push(&head, 's');
    push(&head, '$');
    push(&head, 'k');
    push(&head, 'e');
    push(&head, 'e');
    push(&head, 'g');
  
    cout << "Given linked list: ";
    printList(head);
      
    reverse(&head, 13);
      
    cout << "\nReversed Linked list: ";
    printList(head);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse a 
// linked list without affecting 
// special characters
class GFG
{
  
// Link list node 
public static class Node 
{
    char data;
    Node next;
}
  
// Function to reverse the linked
// list without affecting special 
// characters
static void reverse(Node head_ref,
                    int size)
{
Node current = head_ref;
  
  
char TEMP_ARR[] = new char[size];
  
int i = 0;
  
// Traverse the linked list 
// and insert linked list 
// elements to TEMP_ARR
while (current != null)
{
    // if the cuurent data 
    // is any alphabet than
    // store it in to TEMP_ARR
    if ((current.data >= 97 && 
         current.data <= 122) || 
        (current.data >= 65 && 
         current.data <= 90))
    {
        TEMP_ARR[i++] = current.data;
        current = current.next;
    }
      
    // else increase the node position
    else
        current = current.next;
}
  
current = head_ref;
  
// Traverse the linked list again
while (current != null
{
    // if current character is an 
    // alphabet than replace the 
    // current element in the linked
    // list with the last element 
    // of the TEMP_ARR
    if ((current.data >= 97 && 
         current.data <= 122) || 
        (current.data >= 65 && 
         current.data <= 90)) 
    {
        current.data = TEMP_ARR[--i];
        current = current.next;
    }
      
    // else increase the node
    else
        current = current.next;
    }
}
  
// Function to push a node 
static Node push(Node head_ref,
                 char new_data)
{
    /* allocate node */
    Node new_node = new Node();
  
    /* put in the data */
    new_node.data = new_data;
  
    /* link the old list
       off the new node */
    new_node.next = (head_ref);
  
    /* move the head to point 
       to the new node */
    (head_ref) = new_node;
      
    return head_ref;
}
  
/* Function to print linked list */
static void printList(Node head)
{
    Node temp = head;
    while (temp != null
    {
        System.out.print(temp.data);
        temp = temp.next;
    }
}
  
// Driver Code
public static void main(String rags[])
{
    /* Start with the empty list */
    Node head = null;
  
    head = push(head, 's');
    head = push(head, '$');
    head = push(head, 'k');
    head = push(head, 'e');
    head = push(head, 'e');
    head = push(head, '@');
    head = push(head, '#');
    head = push(head, 'g');
    head = push(head, 'r');
    head = push(head, 'o');
    head = push(head, 'f');
    head = push(head, 's');
    head = push(head, '$');
    head = push(head, 'k');
    head = push(head, 'e');
    head = push(head, 'e');
    head = push(head, 'g');
  
    System.out.print( "Given linked list: ");
    printList(head);
      
    reverse(head, 13);
      
    System.out.print("\nReversed Linked list: ");
    printList(head);
}
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse a 
// linked list without affecting 
// special characters
using System;
  
class GFG
{
  
    // Link list node 
    public class Node 
    {
        public char data;
        public Node next;
    }
  
    // Function to reverse the linked
    // list without affecting special 
    // characters
    static void reverse(Node head_ref,
                        int size)
    {
        Node current = head_ref;
  
        char []TEMP_ARR = new char[size];
  
        int i = 0;
  
        // Traverse the linked list 
        // and insert linked list 
        // elements to TEMP_ARR
        while (current != null)
        {
            // if the cuurent data 
            // is any alphabet than
            // store it in to TEMP_ARR
            if ((current.data >= 97 && 
                current.data <= 122) || 
                (current.data >= 65 && 
                current.data <= 90))
            {
                TEMP_ARR[i++] = current.data;
                current = current.next;
            }
  
            // else increase the node position
            else
                current = current.next;
        }
  
        current = head_ref;
  
        // Traverse the linked list again
        while (current != null
        {
            // if current character is an 
            // alphabet than replace the 
            // current element in the linked
            // list with the last element 
             // of the TEMP_ARR
            if ((current.data >= 97 && 
                current.data <= 122) || 
                (current.data >= 65 && 
                current.data <= 90)) 
            {
                current.data = TEMP_ARR[--i];
                current = current.next;
            }
  
            // else increase the node
            else
                current = current.next;
        }
    }
  
    // Function to push a node 
    static Node push(Node head_ref,
                    char new_data)
    {
        /* allocate node */
        Node new_node = new Node();
  
        /* put in the data */
        new_node.data = new_data;
  
        /* link the old list
        off the new node */
        new_node.next = (head_ref);
  
        /* move the head to point 
        to the new node */
        (head_ref) = new_node;
  
        return head_ref;
    }
  
    /* Function to print linked list */
    static void printList(Node head)
    {
        Node temp = head;
        while (temp != null
        {
            Console.Write(temp.data);
            temp = temp.next;
        }
    }
  
    // Driver Code
    public static void Main(String []rags)
    {
        /* Start with the empty list */
        Node head = null;
  
        head = push(head, 's');
        head = push(head, '$');
        head = push(head, 'k');
        head = push(head, 'e');
        head = push(head, 'e');
        head = push(head, '@');
        head = push(head, '#');
        head = push(head, 'g');
        head = push(head, 'r');
        head = push(head, 'o');
        head = push(head, 'f');
        head = push(head, 's');
        head = push(head, '$');
        head = push(head, 'k');
        head = push(head, 'e');
        head = push(head, 'e');
        head = push(head, 'g');
  
        Console.Write( "Given linked list: ");
        printList(head);
  
        reverse(head, 13);
  
        Console.Write("\nReversed Linked list: ");
        printList(head);
    }
}
  
// This code has been contributed 
// by 29AjayKumar

chevron_right


Output:

Given linked list: geek$sforg#@eek$s
Reversed Linked list: skee$grofs#@kee$g


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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 : andrew1234, 29AjayKumar