From 1c39b2fe2b37a469c06a37e2c946f631c3314e37 Mon Sep 17 00:00:00 2001 From: Benjamin Lohse Date: Tue, 4 Mar 2025 14:47:05 -0500 Subject: [PATCH] Implemented reverseLinkedList function --- main.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index b26f302..ae72647 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,16 @@ class Node { class SinglyLinkedList { private: Node* head; + Node* recucrsiveFunction(Node* p) { + Node* q = p->next; + if (q->next) { + Node* newNode = q->next; + q->next = p; + return recucrsiveFunction(q); + }else { + return nullptr; + } + } public: SinglyLinkedList() : head(nullptr) {} @@ -36,8 +46,17 @@ class SinglyLinkedList { } void reverseLinkedList() { - // TODO: Students will implement this function - std::cout << "Implement reverseLinkedList()" << std::endl; + Node* a = head; + Node* b = a->next; + a->next = nullptr; + while (b->next) { + Node* temp = b->next; + b->next = a; + a = b; + b = temp; + } + b->next = a; + head = b; } };