#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
Node* createLinkedListFromArray(int arr[], int size) {
if (size == 0) return nullptr; // If array is empty, return nullptr
// Create the head node
Node* head = new Node(arr[0]);
Node* current = head;
for (int i = 1; i < size; i++) {
current->next = new Node(arr[i]);
current = current->next;
}
return head;
}
void printLinkedList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
Node* head = createLinkedListFromArray(arr, size);
printLinkedList(head);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCnN0cnVjdCBOb2RlIHsKICAgIGludCBkYXRhOyAgICAgICAgICAgCiAgICBOb2RlKiBuZXh0OyAgICAgICAgCgogICAgCiAgICBOb2RlKGludCB2YWx1ZSkgewogICAgICAgIGRhdGEgPSB2YWx1ZTsKICAgICAgICBuZXh0ID0gbnVsbHB0cjsKICAgIH0KfTsKCgpOb2RlKiBjcmVhdGVMaW5rZWRMaXN0RnJvbUFycmF5KGludCBhcnJbXSwgaW50IHNpemUpIHsKICAgIGlmIChzaXplID09IDApIHJldHVybiBudWxscHRyOyAvLyBJZiBhcnJheSBpcyBlbXB0eSwgcmV0dXJuIG51bGxwdHIKCiAgICAvLyBDcmVhdGUgdGhlIGhlYWQgbm9kZQogICAgTm9kZSogaGVhZCA9IG5ldyBOb2RlKGFyclswXSk7CiAgICBOb2RlKiBjdXJyZW50ID0gaGVhZDsKCiAgICAKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgY3VycmVudC0+bmV4dCA9IG5ldyBOb2RlKGFycltpXSk7CiAgICAgICAgY3VycmVudCA9IGN1cnJlbnQtPm5leHQ7CiAgICB9CgogICAgcmV0dXJuIGhlYWQ7Cn0KC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