给定字符串的链表表示,任务是交换其中的每个后续元音对以生成结果链表。
例子:
Input: List = g -> e -> e -> k -> s -> NULL
Output: g -> e -> e -> k -> s -> NULL
The only vowels are ‘e’ and ‘e’ which
will be swapped with each other.
Input: List = a -> b -> c -> d -> e -> NULL
Output: e -> b -> c -> d -> a -> NULL
swap(a, e)
方法:
- 使用两个链表节点类型的指针分别存储带有第一个元音的节点和遍历链表。
- 开始遍历链表直到结束。
- 如果遇到元音并且第一个指针已经包含在其数据部分中带有元音的节点,则交换当前节点和第一个节点的内容并将第一个指针设置为 NULL。
- 如果遇到元音并且第一个指针不包含任何内容,请将第一个指针设置为指向该节点。
- 如果没有遇到元音,则继续遍历。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Structure to hold the data and
// the address of next element
struct node {
char data;
node* next;
};
// Utility function to add a new
// node to the linked list
node* add(char data)
{
node* newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}
// Function to add the given character
// in the string to the linked list
node* addinll(node* head, char data)
{
// If the linked list is empty then add
// it to the head otherwise add
// the character to the end
if (head == NULL)
head = add(data);
else {
node* curr = head;
while (curr->next != NULL)
curr = curr->next;
curr->next = add(data);
}
return head;
}
// Function to print the linked list
void print(node* head)
{
node* curr = head;
while (curr != NULL) {
cout << curr->data << " -> ";
curr = curr->next;
}
cout << "NULL";
}
// Function that returns true if
// the character is vowel
bool isvowel(char data)
{
if (data == 'a' || data == 'e'
|| data == 'i' || data == 'o'
|| data == 'u')
return true;
if (data == 'A' || data == 'E'
|| data == 'I' || data == 'O'
|| data == 'U')
return true;
return false;
}
// Function to reverse the vowels in the linked list
void reversevowels(node* head)
{
// The pointer named first holds the
// address of the node containing
// the first vowel
node* first = NULL;
// The pointer curr is used
// to point to head
node* curr = head;
// Traverse the linked list
while (curr != NULL) {
// If a vowel is encountered and first is
// set to NULL then update the first
// with the address of this node
if (isvowel(curr->data) and first == NULL)
first = curr;
// If a vowel is encountered and the first
// already contains the address of a vowel
// then swap the current and the first's data
else if (isvowel(curr->data) and first != NULL) {
swap(first->data, curr->data);
// Set the first pointer to NULL
first = NULL;
}
curr = curr->next;
}
}
// Driver code
int main()
{
string s = "geeks";
// Start with an empty linked list
node* head = NULL;
// Add the characters one by
// one to the linked list
for (int i = 0; i < s.size(); i++)
head = addinll(head, s[i]);
// Reverse the vowels
// in the linked list
reversevowels(head);
// Print the linked list
// with reversed vowels
print(head);
return 0;
}
Java
// Java implementation of the approach
class GFG{
// Structure to hold the data and
// the address of next element
static class node
{
char data;
node next;
};
// Utility function to add a new
// node to the linked list
static node add(char data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null;
return newnode;
}
// Function to add the given character
// in the String to the linked list
static node addinll(node head, char data)
{
// If the linked list is empty then add
// it to the head otherwise add
// the character to the end
if (head == null)
head = add(data);
else
{
node curr = head;
while (curr.next != null)
curr = curr.next;
curr.next = add(data);
}
return head;
}
// Function to print the linked list
static void print(node head)
{
node curr = head;
while (curr != null)
{
System.out.print(curr.data + " -> ");
curr = curr.next;
}
System.out.print("NULL");
}
// Function that returns true if
// the character is vowel
static boolean isvowel(char data)
{
if (data == 'a' || data == 'e' ||
data == 'i' || data == 'o' ||
data == 'u')
return true;
if (data == 'A' || data == 'E' ||
data == 'I' || data == 'O' ||
data == 'U')
return true;
return false;
}
// Function to reverse the vowels in the linked list
static void reversevowels(node head)
{
// The pointer named first holds the
// address of the node containing
// the first vowel
node first = null;
// The pointer curr is used
// to point to head
node curr = head;
// Traverse the linked list
while (curr != null)
{
// If a vowel is encountered and first is
// set to null then update the first
// with the address of this node
if (isvowel(curr.data) && first == null)
first = curr;
// If a vowel is encountered and the first
// already contains the address of a vowel
// then swap the current and the first's data
else if (isvowel(curr.data) && first != null)
{
swap(first.data, curr.data);
// Set the first pointer to null
first = null;
}
curr = curr.next;
}
}
private static void swap(char data, char data2)
{
// Auto-generated method stub
char newdata = data;
data = data2;
data2 = newdata;
}
// Driver code
public static void main(String[] args)
{
String s = "geeks";
// Start with an empty linked list
node head = null;
// Add the characters one by
// one to the linked list
for(int i = 0; i < s.length(); i++)
head = addinll(head, s.charAt(i));
// Reverse the vowels
// in the linked list
reversevowels(head);
// Print the linked list
// with reversed vowels
print(head);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 implementation of the approach
# Structure to hold the data and
# the address of next element
class newNode:
def __init__(self, data):
self.data = data
self.next = None
# Utility function to add a new
# node to the linked list
def add(data):
newnode = newNode(data)
return newnode
# Function to add the given character
# in the string to the linked list
def addinll(head, data):
# If the linked list is empty then add
# it to the head otherwise add
# the character to the end
if (head == None):
head = add(data)
else:
curr = head
while (curr.next != None):
curr = curr.next
curr.next = add(data)
return head
# Function to print the linked list
def print1(head):
curr = head
while (curr != None):
print(curr.data,end = " -> ")
curr = curr.next
print("Null")
# Function that returns true if
# the character is vowel
def isvowel(data):
if (data == 'a' or data == 'e' or
data == 'i' or data == 'o' or
data == 'u'):
return True
if (data == 'A' or data == 'E' or
data == 'I' or data == 'O' or
data == 'U'):
return True
return False
# Function to reverse the vowels in the
# linked list
def reversevowels(head):
# The pointer named first holds the
# address of the node containing
# the first vowel
first = None
# The pointer curr is used
# to point to head
curr = head
# Traverse the linked list
while (curr != None):
# If a vowel is encountered and first is
# set to None then update the first
# with the address of this node
if (isvowel(curr.data) and first == None):
first = curr
# If a vowel is encountered and the first
# already contains the address of a vowel
# then swap the current and the first's data
elif (isvowel(curr.data) and first != None):
temp = first.data
first.data = curr.data
curr.data = temp
# Set the first pointer to None
first = None
curr = curr.next
# Driver code
if __name__ == '__main__':
s = "geeks"
# Start with an empty linked list
head = None
# Add the characters one by
# one to the linked list
for i in range(len(s)):
head = addinll(head, s[i])
# Reverse the vowels
# in the linked list
reversevowels(head)
# Print the linked list
# with reversed vowels
print1(head)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# implementation of the approach
using System;
class GFG{
// Structure to hold the data and
// the address of next element
public class node
{
public char data;
public node next;
};
// Utility function to add a new
// node to the linked list
static node add(char data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null;
return newnode;
}
// Function to add the given
// character in the String to
// the linked list
static node addinll(node head,
char data)
{
// If the linked list is
// empty then add it to the
// head otherwise add the
// character to the end
if (head == null)
head = add(data);
else
{
node curr = head;
while (curr.next != null)
curr = curr.next;
curr.next = add(data);
}
return head;
}
// Function to print the linked list
static void print(node head)
{
node curr = head;
while (curr != null)
{
Console.Write(curr.data + " -> ");
curr = curr.next;
}
Console.Write("NULL");
}
// Function that returns true if
// the character is vowel
static bool isvowel(char data)
{
if (data == 'a' || data == 'e' ||
data == 'i' || data == 'o' ||
data == 'u')
return true;
if (data == 'A' || data == 'E' ||
data == 'I' || data == 'O' ||
data == 'U')
return true;
return false;
}
// Function to reverse the vowels
// in the linked list
static void reversevowels(node head)
{
// The pointer named first
// holds the address of the
// node containing the first vowel
node first = null;
// The pointer curr is used
// to point to head
node curr = head;
// Traverse the linked list
while (curr != null)
{
// If a vowel is encountered and first is
// set to null then update the first
// with the address of this node
if (isvowel(curr.data) &&
first == null)
first = curr;
// If a vowel is encountered and
// the first already contains the
// address of a vowel then swap the
// current and the first's data
else if (isvowel(curr.data) &&
first != null)
{
swap(first.data, curr.data);
// Set the first pointer to null
first = null;
}
curr = curr.next;
}
}
private static void swap(char data,
char data2)
{
// Auto-generated method stub
char newdata = data;
data = data2;
data2 = newdata;
}
// Driver code
public static void Main(String[] args)
{
String s = "geeks";
// Start with an empty linked list
node head = null;
// Add the characters one by
// one to the linked list
for(int i = 0; i < s.Length; i++)
head = addinll(head, s[i]);
// Reverse the vowels
// in the linked list
reversevowels(head);
// Print the linked list
// with reversed vowels
print(head);
}
}
// This code contributed by gauravrajput1
输出:
g -> e -> e -> k -> s -> NULL
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live