diff --git a/Hackerrank/reverse-a-linked-list.cpp b/Hackerrank/reverse-a-linked-list.cpp new file mode 100644 index 00000000..6db0acc8 --- /dev/null +++ b/Hackerrank/reverse-a-linked-list.cpp @@ -0,0 +1,23 @@ +/*approach : +We create three pointers to traverse the current previous and the next node, and with each current node, we modify its next pointer and update the pointer again*/ + + +#include +using namespace std; +SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { + if(!llist) return llist; + + SinglyLinkedListNode* prev = nullptr; + SinglyLinkedListNode* curr = llist; + SinglyLinkedListNode* next = nullptr; + + while(curr){ + next = curr->next; + curr->next = prev; + + prev = curr; + curr = next; + } + + return prev; +} diff --git a/README.md b/README.md index 791a2e2c..c31cc8e6 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,9 @@ In this repository, you can find the solutions (as source code) for the problems | [Nauman Chaudhary](https://github.com/nauman-chaudhary)
| Pakistan | Python | | | [Gourav Rusiya](https://github.com/GouravRusiya30)
| India | Java | https://www.hackerrank.com/gouravrusiya786 | | [Trushita Maurya](https://github.com/trushita23)
| India | Java + +| [Arjun Chaudhary](https://github.com/Arjun-3105)
| +India | C++ ### License [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://github.com/ows-ali/Hacktoberfest/blob/master/LICENSE)