👍3🥰1
My request to our group family don't trust anyone simply and don't lose money 🙏🥹
👍2👏1
include <iostream>
struct Node {
int data;
struct Node* next;
};
struct Node* AddAlternateNodes(struct Node* head) {
if (head == nullptr) {
return nullptr;
}
struct Node* curr = head;
while (curr != nullptr && curr->next != nullptr && curr->next->next != nullptr) {
int sum = curr->data + curr->next->next->data;
curr->next->next->data = sum;
curr = curr->next->next;
}
return head;
}
// Function to print the linked list
void PrintList(struct Node* head) {
struct Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// Test case
int main() {
struct Node* head = new Node;
struct Node* second = new Node;
struct Node* third = new Node;
struct Node* fourth = new Node;
struct Node* fifth = new Node;
// Assign data values and connect nodes
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = fifth;
fifth->data = 5;
fifth->next = nullptr;
std::cout << "Original Linked List: ";
PrintList(head);
head = AddAlternateNodes(head);
std::cout << "Modified Linked List: ";
PrintList(head);
// Clean up memory
delete fifth;
delete fourth;
delete third;
delete second;
delete head;
return 0;
}
Alternate nodes in python
struct Node {
int data;
struct Node* next;
};
struct Node* AddAlternateNodes(struct Node* head) {
if (head == nullptr) {
return nullptr;
}
struct Node* curr = head;
while (curr != nullptr && curr->next != nullptr && curr->next->next != nullptr) {
int sum = curr->data + curr->next->next->data;
curr->next->next->data = sum;
curr = curr->next->next;
}
return head;
}
// Function to print the linked list
void PrintList(struct Node* head) {
struct Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// Test case
int main() {
struct Node* head = new Node;
struct Node* second = new Node;
struct Node* third = new Node;
struct Node* fourth = new Node;
struct Node* fifth = new Node;
// Assign data values and connect nodes
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = fifth;
fifth->data = 5;
fifth->next = nullptr;
std::cout << "Original Linked List: ";
PrintList(head);
head = AddAlternateNodes(head);
std::cout << "Modified Linked List: ";
PrintList(head);
// Clean up memory
delete fifth;
delete fourth;
delete third;
delete second;
delete head;
return 0;
}
Alternate nodes in python
👍2
This media is not supported in your browser
VIEW IN TELEGRAM
public class Main {
public static int DesiredArray(int[] Arr, int N, int K) {
int sum = 0;
int count = 0;
int i = 1;
while (count < K) {
boolean divisible = false;
for (int j = 0; j < N; j++) {
if (i % Arr[j] == 0) {
divisible = true;
break;
}
}
if (!divisible) {
sum += i;
count++;
}
i++;
}
return sum;
}
Java
Special Number
public static int DesiredArray(int[] Arr, int N, int K) {
int sum = 0;
int count = 0;
int i = 1;
while (count < K) {
boolean divisible = false;
for (int j = 0; j < N; j++) {
if (i % Arr[j] == 0) {
divisible = true;
break;
}
}
if (!divisible) {
sum += i;
count++;
}
i++;
}
return sum;
}
Java
Special Number
👍2
👍3
🥰2👍1