Реверс четных элементов в связанном списке
Рекурсия, Связанный список
При наличии связанного списка задача состоит в том, чтобы перевернуть смежные четные элементы и распечатать обновленный связанный список.
Input: 1 -> 2 -> 3 -> 3 -> 4 -> 6 -> 8 -> 5 -> NULL
Output: 1 2 3 3 8 6 4 5
Initial list: 1 -> 2 -> 3 -> 3 -> 4 -> 6 -> 8 -> 5 -> NULL
Reversed list: 1 -> 2 -> 3 -> 3 -> 8 -> 6 -> 4 -> 5 -> NULL
Input: 11 -> 32 -> 7 -> NULL
Output: 11 32 7
Подход: Реверсирование смежных четных элементов не произойдет, когда:
- Значение узла нечетное.
- Значение узла является четным, но смежные значения являются нечетными.
В остальных случаях непрерывный блок четных узлов может быть обращен вспять.
Ниже приведена реализация вышеуказанного подхода:
C ++
#include <iostream>
using namespace std;
struct node {
int data;
struct node* next;
};
struct node* newNode( int d)
{
struct node* newnode = new node();
newnode->data = d;
newnode->next = NULL;
return newnode;
}
struct node* reverse( struct node* head, struct node* prev)
{
if (head == NULL)
return NULL;
struct node* temp;
struct node* curr;
curr = head;
while (curr != NULL && curr->data % 2 == 0) {
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
if (curr != head) {
head->next = curr;
curr = reverse(curr, NULL);
return prev;
}
else {
head->next = reverse(head->next, head);
return head;
}
}
void printLinkedList( struct node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
int arr[] = { 1, 2, 3, 3, 4, 6, 8, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
struct node* head = NULL;
struct node* p;
for ( int i = 0; i < n; i++) {
if (head == NULL) {
p = newNode(arr[i]);
head = p;
continue ;
}
p->next = newNode(arr[i]);
p = p->next;
}
head = reverse(head, NULL);
printLinkedList(head);
return 0;
}
|
Джава
import java.util.*;
class GFG
{
static class node
{
int data;
node next;
};
static node newNode( int d)
{
node newnode = new node();
newnode.data = d;
newnode.next = null ;
return newnode;
}
static node reverse(node head, node prev)
{
if (head == null )
return null ;
node temp;
node curr;
curr = head;
while (curr != null && curr.data % 2 == 0 )
{
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
if (curr != head)
{
head.next = curr;
curr = reverse(curr, null );
return prev;
}
else
{
head.next = reverse(head.next, head);
return head;
}
}
static void printLinkedList(node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 3 , 4 , 6 , 8 , 5 };
int n = arr.length;
node head = null ;
node p = new node();
for ( int i = 0 ; i < n; i++)
{
if (head == null )
{
p = newNode(arr[i]);
head = p;
continue ;
}
p.next = newNode(arr[i]);
p = p.next;
}
head = reverse(head, null );
printLinkedList(head);
} }
|
C #
using System;
class GFG
{
public class node
{
public int data;
public node next;
};
static node newNode( int d)
{
node newnode = new node();
newnode.data = d;
newnode.next = null ;
return newnode;
}
static node reverse(node head, node prev)
{
if (head == null )
return null ;
node temp;
node curr;
curr = head;
while (curr != null && curr.data % 2 == 0)
{
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
if (curr != head)
{
head.next = curr;
curr = reverse(curr, null );
return prev;
}
else
{
head.next = reverse(head.next, head);
return head;
}
}
static void printLinkedList(node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 3, 4, 6, 8, 5 };
int n = arr.Length;
node head = null ;
node p = new node();
for ( int i = 0; i < n; i++)
{
if (head == null )
{
p = newNode(arr[i]);
head = p;
continue ;
}
p.next = newNode(arr[i]);
p = p.next;
}
head = reverse(head, null );
printLinkedList(head);
} }
|
Выход:
1 2 3 3 8 6 4 5
Сложность времени: O (N)
Космическая сложность: O (1)
Рекомендуемые посты:
Реверс четных элементов в связанном списке
0.00 (0%) 0 votes