#include
class Node { public: int data; Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class SinglyLinkedList { private: Node* head;
public: SinglyLinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "NULL" << std::endl;
}
void reverseLinkedList() {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current != nullptr) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
};
int main() { SinglyLinkedList list; list.append(1); list.append(2); list.append(3); list.printList();
list.reverseLinkedList();
list.printList();
return 0;
}