Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar04 / reverse-linked-list.cpp
diff options
context:
space:
mode:
Commit3de7b99fbbfdbb44cfddabf6a697d5c3335bdacb (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-10-26 15:43:06 +0300
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-10-26 15:43:06 +0300
Filesa72acd5b4d7b4fcc3da4939e18ef864ee58bc064 /Seminar04/reverse-linked-list.cpp
Data-structures-and-algorithms-3de7b99fbbfdbb44cfddabf6a697d5c3335bdacb.tar
Data-structures-and-algorithms-3de7b99fbbfdbb44cfddabf6a697d5c3335bdacb.tar.gz
Data-structures-and-algorithms-3de7b99fbbfdbb44cfddabf6a697d5c3335bdacb.zip
Parent0480ca20fabdb9373b64c353c90de47e7f97d394 (diff)
Add Seminar04
1 files changed, 25 insertions, 0 deletions
-rw-r--r--Seminar04/reverse-linked-list.cpp25
diff --git a/Seminar04/reverse-linked-list.cpp b/Seminar04/reverse-linked-list.cpp
new file mode 100644
index 0000000..486b41e
--- /dev/null
+++ b/Seminar04/reverse-linked-list.cpp
@@ -0,0 +1,25 @@
+#include "linked-list-utils.h"
+
+template<class T>
+void reverseLinkedList(LinkedListNode<T>*& ll) {
+ LinkedListNode<T>* previous = nullptr;
+ LinkedListNode<T>* next = nullptr;
+ LinkedListNode<T>* iterate = ll;
+
+ while(iterate) {
+ next = iterate->next;
+ iterate->next = previous;
+ previous = iterate;
+ iterate = next;
+ }
+
+ ll = previous;
+}
+
+void example() {
+ LinkedListNode<int>* ll = new LinkedListNode(1, new LinkedListNode(2, new LinkedListNode(3, new LinkedListNode(4))));
+ printLinkedList(ll);
+ reverseLinkedList(ll);
+ printLinkedList(ll);
+ freeList(ll);
+} \ No newline at end of file