#include <bits/stdc++.h>
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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKc3RydWN0IE5vZGUgewogICAgaW50IGRhdGE7ICAgICAgICAgICAKICAgIE5vZGUqIG5leHQ7ICAgICAgICAKCiAgICAKICAgIE5vZGUoaW50IHZhbHVlKSB7CiAgICAgICAgZGF0YSA9IHZhbHVlOwogICAgICAgIG5leHQgPSBudWxscHRyOwogICAgfQp9OwoKCk5vZGUqIGNyZWF0ZUxpbmtlZExpc3RGcm9tQXJyYXkoaW50IGFycltdLCBpbnQgc2l6ZSkgewogICAgaWYgKHNpemUgPT0gMCkgcmV0dXJuIG51bGxwdHI7IC8vIElmIGFycmF5IGlzIGVtcHR5LCByZXR1cm4gbnVsbHB0cgoKICAgIC8vIENyZWF0ZSB0aGUgaGVhZCBub2RlCiAgICBOb2RlKiBoZWFkID0gbmV3IE5vZGUoYXJyWzBdKTsKICAgIE5vZGUqIGN1cnJlbnQgPSBoZWFkOwoKICAgIAogICAgZm9yIChpbnQgaSA9IDE7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBjdXJyZW50LT5uZXh0ID0gbmV3IE5vZGUoYXJyW2ldKTsKICAgICAgICBjdXJyZW50ID0gY3VycmVudC0+bmV4dDsKICAgIH0KCiAgICByZXR1cm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