#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;
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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCnN0cnVjdCBOb2RlIHsKICAgIGludCBkYXRhOyAgICAgICAgICAgCiAgICBOb2RlKiBuZXh0OyAgICAgICAgIAoKICAgIAogICAgTm9kZShpbnQgdmFsdWUpIHsKICAgICAgICBkYXRhID0gdmFsdWU7CiAgICAgICAgbmV4dCA9IG51bGxwdHI7CiAgICB9Cn07CgoKTm9kZSogY3JlYXRlTGlua2VkTGlzdEZyb21BcnJheShpbnQgYXJyW10sIGludCBzaXplKSB7CiAgICBpZiAoc2l6ZSA9PSAwKSByZXR1cm4gbnVsbHB0cjsKCiAgIAogICAgTm9kZSogaGVhZCA9IG5ldyBOb2RlKGFyclswXSk7CiAgICBOb2RlKiBjdXJyZW50ID0gaGVhZDsKCiAgICAKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgY3VycmVudC0+bmV4dCA9IG5ldyBOb2RlKGFycltpXSk7CiAgICAgICAgY3VycmVudCA9IGN1cnJlbnQtPm5leHQ7CiAgICB9CgogICAgcmV0dXJuIGhlYWQ7Cn0KCgp2b2lkIHByaW50TGlua2VkTGlzdChOb2RlKiBoZWFkKSB7CiAgICBOb2RlKiBjdXJyZW50ID0gaGVhZDsKICAgIHdoaWxlIChjd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