#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void reverseLinkedList(Node*& head) {
Node* prev = nullptr;
Node* current = head;
Node* nextNode = nullptr;
while (current != nullptr) {
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
head = prev;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " ";
head = head->next;
}
std::cout << std::endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
std::cout << "Original Linked List: ";
printList(head);
reverseLinkedList(head);
std::cout << "Reversed Linked List: ";
printList(head);
return 0;
}