📜  在不影响特殊字符的情况下反转链表的节点

📅  最后修改于: 2022-05-13 01:57:43.461000             🧑  作者: Mango

在不影响特殊字符的情况下反转链表的节点

给定一个字母表和特殊字符的链表。反转给定的链表而不影响特殊字符的位置。

例子:

这个想法是遍历链表存储排除在临时数组中的特殊字符的字符。再次遍历链表,以相反的方式将数组中的元素复制到链表的节点。

以下是分步算法



  1. 取一个临时数组 TEMP_ARR。
  2. 遍历链表并执行以下操作
    • 如果当前元素是字母表,则将该链表的元素存储到 TEMP_ARR。
    • 否则,将节点指针加一
  3. 再次从头部开始遍历链表,从尾部开始遍历 TEMP_ARR 并执行以下操作:
    • 如果当前元素是字母表,则将 TEMP_ARR 的最后一个元素复制到当前链表节点,并为下一次迭代减小 TEMP_ARR 的当前索引。
    • 否则,将节点增加一

下面是上述方法的实现:

C++
// C++ program to reverse a linked list
// without affecting special characters
 
#include 
 
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 current 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;
}


Java
// 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 current 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


Python3
# Python3 program to reverse a linked list
# without affecting special characters
 
# Link list node
class Node:
     
    def __init__(self, x):
         
        self.data = x
        self.next = None
 
# Function to reverse the linked list
# without affecting special characters
def reverse(head_ref, size):
     
    current = head_ref
    TEMP_ARR = [0 for i in range(256)]
 
    i = 0
 
    # Traverse the linked list and insert
    # linked list elements to TEMP_ARR
    while (current != None):
         
        # If the current data is any alphabet than
        # store it in to TEMP_ARR
        if ((ord(current.data) >= 97 and
             ord(current.data) <= 122) or
            (ord(current.data) >= 65 and
             ord(current.data) <= 90)):
            TEMP_ARR[i]= current.data
            i += 1
            current = current.next
             
        # Else increase the node position
        else:
            current = current.next
 
    current = head_ref
     
    # Traverse the linked list again
    while (current != None):
         
        # If current character is an alphabet
        # than replace the current element in
        # the linked list with the last element
        # of the TEMP_ARR
        if ((ord(current.data) >= 97 and
             ord(current.data) <= 122) or
            (ord(current.data) >= 65 and
             ord(current.data) <= 90)):
            i = i - 1
            current.data = TEMP_ARR[i]
            current = current.next
             
        # Else increase the node
        else:
            current = current.next
             
    return head_ref
 
# Function to push a node
def push(head_ref, new_data):
     
    # Allocate node
    #new_node = (struct Node*)malloc(sizeof(struct Node));
 
    # Put in the data
    new_node = Node(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
def printList(head):
     
    temp = head
     
    while (temp != None):
        print(temp.data, end = "")
        temp = temp.next
 
# Driver code
if __name__ == '__main__':
     
    # Start with the empty list
    head = None
 
    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')
 
    print("Given linked list: ", end = "")
    printList(head)
 
    head = reverse(head, 13)
 
    print("\nReversed Linked list: ", end = "")
    printList(head)
 
# This code is contributed by mohit kumar 29


C#
// 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 current 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


Javascript


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

时间复杂度: O(N),其中 N 是链表中的节点总数。
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程