From 26748187faed1febf81678c729476abcafc09ac3 Mon Sep 17 00:00:00 2001 From: ElissaBD Date: Tue, 4 Mar 2025 14:59:45 -0500 Subject: [PATCH] Update main.cpp --- main.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index b26f302..2f6c2de 100644 --- a/main.cpp +++ b/main.cpp @@ -36,8 +36,18 @@ class SinglyLinkedList { } void reverseLinkedList() { - // TODO: Students will implement this function - std::cout << "Implement reverseLinkedList()" << std::endl; + Node* prev = nullptr; + Node* current = head; + Node* next = nullptr; + + while (current != nullptr) { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + + head = prev; } }; @@ -48,7 +58,6 @@ int main() { list.append(3); list.printList(); - // Student should implement reverseLinkedList() list.reverseLinkedList(); list.printList();